Typescript Basics & Basic Types

Teasanยท2022๋…„ 9์›” 14์ผ
0

typescript

๋ชฉ๋ก ๋ณด๊ธฐ
13/13
post-thumbnail

๋ชฉ์ฐจ

  • rootDir and outDir
  • Stop Emitting Files on Compilation Errors
  • Strict ์ปดํŒŒ์ผ
  • ์ฝ”๋“œ ํ’ˆ์งˆ ์˜ต์…˜

โœง rootDir ๋ฐ outDir

  • dist ํด๋”๋Š” ๋ชจ๋“  ์ถœ๋ ฅ๊ฐ’์„ ๋ณด๊ด€ํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ชจ๋“  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๊ณผ src ํด๋”์— ๋ชจ๋“  ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์ด ๋ณด๊ด€๋˜๋ฏ€๋กœ dist์™€ src ํด๋”๋ฅผ ์…์„ฑํ•˜๊ณ , ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ „๋ถ€ src ํด๋”๋กœ ์˜ฎ๊ธด๋‹ค. ๋ฃจํŠธ ์œ„์น˜์— ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํด๋”๋ฅผ ์‚ญ์ œํ•˜๊ณ  tsc ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ .ts ํŒŒ์ผ์„ ๋‹ค์‹œ ์ปดํŒŒ์ผํ•œ๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋””์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํ•˜์œ„ ํด๋”๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด์ง€๋งŒ, ์ถœ๋ ฅ๊ฐ’์€ ์ž…๋ ฅํŒŒ์ผ ์˜†์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ž…๋ ฅ ํŒŒ์ผ์„ outDir๋กœ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค.
{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
  }
}
  • ์˜ˆ๋ฅผ ๋“ค์–ด, outDir๋ฅผ ์„ค์ •ํ•˜๋ฉด, ์ƒ์„ฑ๋œ ํŒŒ์ผ์ด ์ €์žฅ๋˜๋Š” ์œ„์น˜๋ฅผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ์— ์•Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ dist๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ํ›„์— tsc ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์ด src ํด๋”๊ฐ€ ์•„๋‹Œ, dist ํด๋”์— ์ƒ์„ฑ๋œ๋‹ค.
<script src="app.js" defer></script>
<script src="analytics.js" defer></script>
  • ์ด์ œ index.html๋กœ ์ด๋™ํ•˜์—ฌ app.js๋ฅผ dist/app.js๋กœ, analytics.js๋ฅผ dist/analytics.js๋กœ ์กฐ์ •ํ•œ๋‹ค. index.html ํŒŒ์ผ์„ dist ํด๋”๋กœ ์ด๋™์‹œํ‚จ๋‹ค. ์ด์ œ dev ์„œ๋ฒ„๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค.
<script src="dist/app.js" defer></script>
<script src="dist/analytics.js" defer></script>
  • ์ด์ œ ๊ฐ€์ ธ์˜จ ํ•ญ๋ชฉ๋“ค์„ ์กฐ์ •ํ•ด๋ณด์ž. ์ž‘์—… ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ๊ฐ€ ๋” ๊น”๋”ํ•ด์กŒ๋‹ค.

image

  • ๋งŒ์•ฝ analytics.ts ํŒŒ์ผ์„ ๋™์ผํ•œ ์ด๋ฆ„์˜ ํด๋”๋กœ ๋งŒ๋“ค์–ด์„œ ๊ทธ ์•ˆ์— ๋„ฃ์–ด๋‘๊ณ  tsc ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค๊ณ  ํ•ด๋„, ์ด์ œ ์ปดํŒŒ์ผ๋œ js ํŒŒ์ผ์€ ์•Œ์•„์„œ dist ํด๋” ์•ˆ์—์„œ ์ƒ์„ฑ๋  ๊ฒƒ์ด๋‹ค.
{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
  }
}
  • ์ด์ œ rootDir ๋ฅผ ์„ค์ •ํ•˜๊ณ  src์™€ ๊ฐ™์ด ํŒŒ์ผ์ด ์ €์žฅ๋˜๋Š” ํด๋”๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ๊ฒฝ๋กœ ์„ค์ •ํ•˜์—ฌ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํด๋”์—์„œ ๋ณด์ด์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. (์ด๋Š” ๋ฐ‘์— ์žˆ๋Š” include ์˜ต์…˜์œผ๋กœ๋„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ž‘์—…์ด๋‹ค.) rootDir ์„ค์ •์—์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” src ํด๋”๋„ ํ™•์ธํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์„ค์ •ํ•œ ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ๊ฐ€ dist ํด๋”์—์„œ ์œ ์ง€๋˜๊ณ  ์žˆ๋Š”์ง€๋„ ํ™•์ธํ•œ๋‹ค. ์ด ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  outDir๋ฅผ ์„ค์ •ํ•˜๊ฒŒ ๋˜๋ฉด, ๋ชจ๋“  ํŒŒ์ผ์— dist ์ƒ์„ฑ์ด ์ ์šฉ๋˜๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

  • ๋”ฐ๋ผ์„œ rootDir์™€ outDir๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ž…๋ ฅํŒŒ์ผ์ด ์žˆ๋Š” ์œ„์น˜์™€ ์ถœ๋ ฅ ํŒŒ์ผ์ด ์ƒ์„ฑ๋  ์œ„์น˜์— ๋Œ€ํ•ด ์ •ํ™•ํ•˜๊ฒŒ ์„ค์ •์„ ํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    ...
  }
}
  • removeComments ์— ๋Œ€ํ•ด์„œ๋„ ์‚ดํŽด๋ณด์ž. ์ด๋Š” ์ด๋ฆ„๋งŒ์œผ๋กœ๋„ ๊ธฐ๋Šฅ์ด ์ง์ž‘ ๊ฐ€๋Šฅํ•˜๋‹ค. removeComments ์˜ต์…˜์„ ์„ค์ •ํ•˜๋ฉด, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์˜ ๋ชจ๋“  ์ฃผ์„์ด ์ปดํŒŒ์ผ๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์—์„œ '์ œ๊ฑฐ' ๋œ๋‹ค! ์ด๋Š” ํŒŒ์ผ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š”๋ฐ ์ข‹์€ ์˜ต์…˜์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    // "noEmit": true,

    ...
  }
}
  • noEmit์€ ์–ด๋–จ๊นŒ? noEmit์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์„ ๋•Œ ์„ค์ •ํ•˜๋Š” ์˜ต์…˜์ด๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๊ฐœ๋…์ด๋ผ์„œ ์กฐ๊ธˆ ์ด์ƒํ•˜๊ฒŒ ๋ณด์ด๊ฒ ์ง€๋งŒ, ์ด ์˜ต์…˜์€ ํŒŒ์ผ์ด ์ •ํ™•ํ•œ์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ์ง€๋งŒ ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ์ถœ๋ ฅ ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ทœ๋ชจ๊ฐ€ ํฐ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์ด ๊ฐ’์„ ์ฐธ(true)์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ์ถœ๋ ฅ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค์ง€ ์•Š๊ณ ๋„ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํŒŒ์ผ์„ ๊ฒ€์‚ฌํ•˜๊ณ  ์ž ์žฌ์  ์—๋Ÿฌ๋ฅผ ๋ณด๊ณ ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    // "noEmit": true,
    // "downlevelIteration": true,
    ...
  }
}
  • downlevelIteration ์˜ต์…˜์€ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ์ด๋‹ค. for ๋ฃจํ”„๊ฐ€ ์žˆ๊ณ  ์ƒ์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ํ•ด๋‹น ๋ฃจํ”„์™€ ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ํ•ด๋‹น ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ•œ๋‹ค.

โœง ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ์‹œ ํŒŒ์ผ ๋ฐฉ์ถœ ์ค‘์ง€ํ•˜๊ธฐ

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    ...
    "noEmitOnError": true,
    ...
  }
}
  • noEmitOnError ์˜ต์…˜์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์— ์—๋Ÿฌ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ(์ปดํŒŒ์ผ)์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์ง€ ์•Š์„ ๋•Œ ์„ค์ •ํ•œ๋‹ค.

โœง Strict ์ปดํŒŒ์ผ

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    ...
    "noEmitOnError": true,
    ...
    "strict": true
  }
}
  • strict ์˜ต์…˜์€ ์ฝ”๋“œ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์—…ํ•˜๊ณ  ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ฐ’์„ ๋ช…ํ™•ํžˆ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค. ์ด "strict": true ์˜ต์…˜์œผ๋กœ ๋ชจ๋“  "strict" ์œ ํ˜•์˜ ๊ฒ€์‚ฌ ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์˜ต์…˜์• ์„ค์ •ํ•˜๋ฉด ๋ชจ๋“  ์˜ต์…˜์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์œผ๋ฏ€๋กœ ์˜ต์…˜์„ ํ•˜๋‚˜์”ฉ ์„ค์ •ํ•˜๊ฑฐ๋‚˜, "strict": true ๋งŒ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    ...
    "noEmitOnError": true,
    ...
    // "strict": true,
    "noImplicitAny": false
  }
}

noImplicitAny

  • ๋จผ์ €, noImplicitAny ์˜ต์…˜๋ถ€ํ„ฐ ์‚ดํŽด๋ณด์ž. ์ด ์˜ต์…˜์€ ๋” ๋‚˜์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ์•„์ฃผ ํฅ๋ฏธ๋กœ์šด ์˜ต์…˜์ด๋‹ค.
function sendAnalytics(data) {
  // data์—์„œ ํƒ€์ž…์—๋Ÿฌ ๋ฐœ์ƒ
  console.log(data);
}

sendAnalytics("The data");
  • ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด, ๋ฒŒ์จ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์—๋Ÿฌ๋ฅผ ๋ณด๊ณ  ํ•˜๊ฒŒ ๋œ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ๋Š” ์•”๋ฌต์ ์œผ๋กœ any ํƒ€์ž…์„ ์ง€๋‹ˆ๊ธฐ์— ํ•ด๋‹น ์˜ต์…˜๊ณผ ์—ฐ๊ด€๋œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ํ•ด๋‹น ์˜ต์…˜์„ false ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ๋Š” IDE ์—์„œ๋‚˜ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ ํ•  ๋•Œ๋„ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿผ ์ด ์˜ต์…˜์˜ ๊ธฐ๋Šฅ์€ ๋ฌด์—‡์ผ๊นŒ? ์ด ์˜ต์…˜์€ ์ฝ”๋“œ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์—…ํ•˜๊ณ  ์žˆ๋Š” '๋งค๊ฐœ๋ณ€์ˆ˜'์™€ '๊ฐ’'์„ ๋ช…ํ™•ํžˆ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.
function sendAnalytics(data) {
  // data์—์„œ ํƒ€์ž…์—๋Ÿฌ ๋ฐœ์ƒ
  console.log(data);
}

sendAnalytics("The data");
  • ์—ฌ๊ธฐ์„œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ์„œ ์–ป๋Š” ๋ฐ์ดํ„ฐ ์œ ํ˜•์— ๋Œ€ํ•œ ์–ด๋–ค ์ •๋ณด๋„ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์— ์ œ๊ณต๋˜์ง€ ์•Š๋Š”๋‹ค. ๋งŒ์•ฝ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ด๋ฅผ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋‹น์—ฐํžˆ ๊ดœ์ฐฎ๋‹ค. ํ•˜์ง€๋งŒ ์–ด๋–ค ํƒ€์ž…์ด ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ํ™•์‹คํ•œ ๊ฑด ํ•ด๋‹น ํŒŒ์ผ์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— sendAnalytics์ด ๋จผ์ € ์„ ์–ธ๋œ๋‹ค๋Š” ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์‹œ์ ์—๋Š” ์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚ ์ง€ ์•Œ ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.
function sendAnalytics(data: string) {
  // data์—์„œ ํƒ€์ž…์—๋Ÿฌ ๋ฐœ์ƒ
  console.log(data);
}

sendAnalytics("The data");
  • ๊ทธ๋ ‡๊ธฐ์— ํƒ€์ž…์„ ์„ ์–ธํ•˜๊ณ  ์—ฌ๊ธฐ์„œ ์–ด๋–ค ํƒ€์ž…์„ ์‚ฌ์šฉํ• ์ง€ ๋ช…ํ™•ํžˆ ํ•จ์œผ๋กœ์จ ํƒ€์ž…์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
let logged;

function sendAnalytics(data: string) {
  console.log(data);
  logged = true;
}

sendAnalytics("The data");
  • ๊ทธ๋ฆฌ๊ณ  logged ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ฐ’์„ ํ•จ์ˆ˜ ์•ˆ์—์„œ true ๋กœ ์„ค์ •ํ•˜๋ฉด ํ•ด๋‹น ์„ ์–ธ์— ๋Œ€ํ•œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

strictNullChecks

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    ...
    "noEmitOnError": true,
    ...
    "strict": true,
    "strictNullChecks": false,
  }
}
  • strictNullChecks ์˜ต์…˜์€ null ๊ฐ’์„ ์ž ์žฌ์ ์œผ๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์— ์ ‘๊ทผํ•˜๊ณ  ์ž‘์—…ํ•˜๋Š” ๋ฐฉ์‹์„ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—๊ฒŒ ๋งค์šฐ ์—„๊ฒฉํ•˜๊ฒŒ ์•Œ๋ ค์ค€๋‹ค.

app.js

const button = document.querySelector("button")!;

button.addEventListener("click", () => {
  console.log("Clicked");
});
  • button ์—ญ์‹œ null์ผ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‰ฌ์šด ํ•ด๊ฒฐ์ฑ…์€ ๋Š๋‚Œํ‘œ ์—ฐ์‚ฐ์ž๋ฅผ ๋’ค์— ๋ถ™์ด๋Š” ์ผ์ด๋‹ค.
const button = document.querySelector("button"); // ! ํƒ€์ž…์„ ์ง€์› ๋‹ค.
  • selector๊ฐ€ ์žˆ๋”๋ผ๋„, ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜๋Š” ํŽ˜์ด์ง€์— ๋ฒ„ํŠผ์ด ์—†์„ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” html ํŒŒ์ผ์„ ์‚ดํŽด๋ณด์ง€ ์•Š์œผ๋ฏ€๋กœ, ์„ฑ๊ณต์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ด ์ฝ”๋“œ๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ํ™•๋ฅ ์ด ๋†’๋‹ค.
const button = document.querySelector("button"); // ! ํƒ€์ž…์„ ์ง€์› ๋‹ค.

button.addEventListener("click", () => {
  console.log("Clicked");
});
  • ๋งŒ์•ฝ index.html์˜ ๋ฒ„ํŠผ ๋ถ€๋ถ„์„ ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜๊ณ ,
<body>
  <!-- <button>Click me</button> -->
</body>
  • ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค. addEventListener๋ฅผ null๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•ด๋‹น ๋ฒ„ํŠผ์„ ์ฃผ์„์ฒ˜๋ฆฌ ํ–ˆ์œผ๋ฏ€๋กœ button์€ ์‚ฌ๋ผ์กŒ๊ณ ,
const button = document.querySelector("button"); // ! ํƒ€์ž…์„ ์ง€์› ๋‹ค.
  • button์ด ์—†์œผ๋ฏ€๋กœ ์ด ๊ฐ’์€ null์ด ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Š” strictNullChecks๋ฅผ true๋กœ ์„ค์ •ํ•˜์—ฌ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์ˆ˜๋‹ค.
{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    ...
    "noEmitOnError": true,
    ...
    "strict": true,
    "strictNullChecks": true,
  }
}
  • strict๋ฅผ true๋กœ ์„ค์ •ํ•˜๋ฉด strictNullCheck๋„ ์ž๋™์œผ๋กœ ์„ค์ •๋˜์–ด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์œ„์˜ ๋ถˆ์ƒ์‚ฌ๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค. ๋ฌผ๋ก  ์—ฌ๊ธฐ์„œ ์‰ฌ์šด ํ•ด๊ฒฐ์ฑ…์€ ๋Š๋‚Œํ‘œ ์—ฐ์‚ฐ์ž์ด์ง€๋งŒ ๋ง์ด๋‹ค.
"strict": true,
// "strictNullChecks": true,
// strict๋ฅผ true๋กœ ์„ค์ •ํ•˜๋ฉด `strictNullCheck`๋„ ์ž๋™์œผ๋กœ ์„ค์ •
  • ๋Š๋‚Œํ‘œ ! ์—ฐ์‚ฐ์ž๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ํ•ด๋‹น ๋ฒ„ํŠผ์ด ์กด์žฌํ•˜๊ฑฐ๋‚˜ ์ด ์—ฐ์‚ฐ์ด null์ด ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋ณดํ†ต html ์ฝ”๋“œ๋ฅผ ์ž‘์—…ํ•˜๋ฉด์„œ button ์—˜๋ฆฌ๋จผํŠธ, ์ฆ‰ ์—ฌ๊ธฐ ์žˆ๋Š” selector๊ฐ€ ์ž‘๋™๋˜๋Š” button์ด ์žˆ์Œ์„ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ์— ๊ทธ์ € ๋Š๋‚Œํ‘œ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋  ๊ฒƒ์ด๋‹ค.
const button = document.querySelector("button");!
  • ์ž‘๋™ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์•Œ ์ˆ˜ ์—†๋Š”๋ฐ ์ž‘๋™์ด ์ž˜ ๋˜๊ธฐ๋ฅผ ์›ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” if ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ์žˆ์„์ง€๋„ ๋ชจ๋ฅด๋Š” ์ฝ”๋“œ๋ฅผ ๊ฐ์‹ธ๋Š” ๊ฒŒ ์ข‹๋‹ค.
const button = document.querySelector("button");

if (button) {
  // truthy
  button.addEventListener("click", () => {
    console.log("Clicked");
  });
}
  • ์ด๋ ‡๊ฒŒ if๋ฌธ์œผ๋กœ ๊ฐ์‹ธ๊ฒŒ ๋˜๋ฉด, ๋Š๋‚Œํ‘œ ์—ฐ์‚ฐ์ž๊ฐ€ ์—†์–ด๋„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์€ ๊น”๋”ํ•ด๋ณด์ด์ง€๋งŒ ๋ฌด์–ธ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์ด ํ™•์‹คํ•˜๋‹ค๋ฉด ๋Š๋‚Œํ‘œ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฝค ๊ดœ์ฐฎ์€ ์˜ต์…˜์ด๋‹ค.
{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    ...
    "noEmitOnError": true,
    ...
    "strict": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

  }
}
  • strictFunctionTypes์€ ๋ณด๋‹ค ๊ณ ๊ธ‰ ์„ค์ •์œผ๋กœ, ๋งŽ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ณผ ์ˆ˜ ์—†๋Š” ํ‹ˆ์ƒˆ ๋ฐ•์Šค๋ฅผ ์žก๋Š”๋‹ค. ์ด๋Š” ์„ค์ • ์ค‘์ธ ํ•จ์ˆ˜ ํƒ€์ž…๊ณผ ๊ด€๋ จ์ด ์žˆ์œผ๋ฉฐ, ํ•จ์ˆ˜ ๋‚ด์˜ ํƒ€์ž…์€ ์•„๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•ด ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ํ‘œ์‹œ๋˜๋Š”์ง€ ์ •์˜๋˜๊ณ , ํ•จ์ˆ˜ ํƒ€์ž…์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ•์Šค๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6"
    "module": "commonjs"
    "lib": [
      "DOM",
      "ES6",
      "DOM.Iterable",
      "ScriptHost"
    ]
    /* Specify library files to be included in the compilation. */,

    ...
    // "outFile": "./", /* Concatenate and emit output to single file. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    ...
    "removeComments": true,
    ...
    "noEmitOnError": true,
    ...
    "strict": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

  }
}
  • strictBindCallApply๋Š” ๊ฒฐํ•ฉํ•˜๊ณ  ํ˜ธ์ถœํ•˜๊ณ  ์ ์šฉํ•˜๋Š” ์ž‘์—…์— ์œ ์šฉํ•˜๋‹ค.
const button = document.querySelector("button");

function clickHandler() {
  console.log("Clicked");
}

if (button) {
  button.addEventListener("click", clickHandler);
}
  • addEventListener ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ๋กœ clickHanlder ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ, ํŠน์ • ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ์–ด๋–ค ํ‚ค์›Œ๋“œ๋ฅผ ํŠน์ • ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ณ ์ž ํ•œ๋‹ค.
const button = document.querySelector("button");

function clickHandler(message: string) {
  console.log("Clicked" + message);
}

if (button) {
  button.addEventListener("click", clickHandler);
}
  • ๋ฌธ์ž์—ด ์ธ์ž๋ฅผ ๋ฐ›๋Š” clickHandler ํ•จ์ˆ˜์— ์ „๋‹ฌํ•  ์ธ์ž๋ฅผ ์žฌ๊ตฌ์„ฑํ•˜๊ณ ์ž ํ•  ๋•Œ bind()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. bind()๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ด ํ‚ค์›Œ๋“œ์— ๊ฒฐํ•ฉ(bind)ํ•˜๊ณ ์ž ํ•˜๋Š” ํ•ญ๋ชฉ๋„ ์ทจํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜์ด๋‹ค.
const button = document.querySelector("button");

function clickHandler(message: string) {
  console.log("Clicked" + message);
}

if (button) {
  button.addEventListener("click", clickHandler.bind(null));
}
  • null ์ด๋ž€ ๊ฐ’์— ๊ฒฐํ•ฉ(bind)ํ•˜๊ณ ์ž ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์—๋Ÿฌ๋Š” strictBindCallApply๋ฅผ false๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์˜ต์…˜์˜ ๊ธฐ๋Šฅ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋ ค๋Š” ํ•จ์ˆ˜๊ฐ€ bind, call, apply ์ค‘ ๋ฌด์—‡์— ํ•ด๋‹นํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์—ฌ๊ธฐ์— ํ•จ์ˆ˜๋ฅผ ์ œ๋Œ€๋กœ ์„ค์ •ํ–ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค. bind๊ฐ€ ์ง€์ •๋œ clickHandler์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ด๋ฅผ ๊ตฌ์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. bind, call, apply ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์‹ค์ˆ˜ํ•˜์ง€ ์•Š๋„๋กํ•ด์ฃผ๋Š” ์•„์ฃผ ์œ ์šฉํ•œ ์˜ต์…˜์ด๋‹ค.


โœง ์ฝ”๋“œ ํ’ˆ์งˆ ์˜ต์…˜

  "noUnusedLocals": true,                /* Report errors on unused locals. */
  "noUnusedParameters": true,            /* Report errors on unused parameters. */
  "noImplicitReturns": true,
  • ์ด ์„ธ๊ฐ€์ง€ ์˜ต์…˜์€ ์ฝ”๋“œ ํ’ˆ์งˆ์„ ํ–ฅ์ƒ ์‹œ์ผœ์ฃผ๋Š” ์˜ต์…˜๋“ค๋กœ, ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์—๋Ÿฌ๋ฅผ ํ‘œ์‹œํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ด ์„ธ๊ฐ€์ง€ ์˜ต์…˜์„ true๋กœ ์„ค์ •ํ•ด๋†“์œผ๋ฉด break ํ‚ค์›Œ๋“œ๋ฅผ ์žŠ์–ด๋ฒ„๋ฆด ์ˆ˜๋„ ์žˆ๋Š” switch ๋ฌธ์„ ์ž…๋ ฅํ•˜๋„๋ก ๋„์™€์ค€๋‹ค. ๋นจ๊ฐ„ ์ƒ‰์ด ์•„๋‹Œ ๋…ธ๋ž€์ƒ‰ ์„ ์œผ๋กœ ํ‘œ์‹œ๋˜๋Š” ์ด์œ ๋Š” ์‚ฌ์‹ค์ƒ ์—๋Ÿฌ๋ผ๊ธฐ ๋ณด๋‹ค๋Š” ๊ฒฝ๊ณ ๋‚˜ ํžŒํŠธ์— ๋” ๊ฐ€๊น๋‹ค. ๋”ฐ๋ผ์„œ ์ปดํŒŒ์ผ ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์—๋Ÿฌ๋กœ๋งŒ ํ‘œ์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ง€์—ญ ๋ณ€์ˆ˜๋‚˜ ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์›์น˜์•Š๋Š”๋‹ค๊ณ  ํ‘œ์‹œํ•  ๊ฒƒ์ด๋‹ค.

โœฆ ์ถœ์ฒ˜


๐Ÿšจ ํ•ด๋‹น ํฌ์ŠคํŒ…์€ Udemy์˜ Typescript :๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‹ค์ „ํ˜• ํ”„๋กœ์ ํŠธ๊นŒ์ง€ with React + NodeJS ๊ฐ•์˜๋ฅผ ๋ฒ ์ด์Šค๋กœ ํ•œ ๊ธฐ๋ก์ž…๋‹ˆ๋‹ค.
โœ๐Ÿป ๊ฐ•์˜ git repo ๋ฐ”๋กœ๊ฐ€๊ธฐ

profile
์ผ๋‹จ ๊ณต๋ถ€๊ฐ€ '์ ์„ฑ'์— ๋งž๋Š” ๊ฐœ๋ฐœ์ž. ๊ทผ์„ฑ์žˆ์Šต๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€