typescript-handbook-01-the-basics.md

๐Ÿก

Typescript Handbook

The Basics

// Accessing the property 'toLowerCase'
// on 'message' and then calling it
message.toLowerCase();
// 1. Is message has a property 'toLowerCase' on it?
// 2. Is 'toLowerCase' is callable?
// 3. What do 'toLowerCase' return?

// Calling 'message'
message(); // ๐Ÿค” Is message callable?
  • JS์—์„œ๋Š” ์‹คํ–‰ ํ–ˆ์„ ๋•Œ(๋Ÿฐํƒ€์ž„์—์„œ) ํƒ€์ž… ์˜ค๋ฅ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค
    • TypeError: message is not a function
  • JavaScript ๋Ÿฐํƒ€์ž„์€ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์ž์‹ ์ด ๋ฌด์—‡์„ ํ•ด์•ผ ํ• ์ง€ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๊ฐ’์˜ ํƒ€์ž…, ์ฆ‰ ํ•ด๋‹น ๊ฐ’์ด ์–ด๋–ค ๋™์ž‘๊ณผ ๋Šฅ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค
  • ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ, ์‹คํ–‰ ์‹œ์ ์—์„œ ํ•ด๋‹น ๊ฐ’์˜ ํƒ€์ž…์„ ํ™•์ธํ•˜๋Š” ๋งค์ปค๋‹ˆ์ฆ˜ x

    or some values, such as the primitives string and number, we can identify their type at runtime using the typeof operator. But for other things like functions, thereโ€™s no corresponding runtime mechanism to identify their types. For example, consider this function:

Static type-checking, ์ •์  ํƒ€์ž… ๊ฒ€์‚ฌ

  • a type is the concept of describing which values can be passed to fn and which will crash, JavaScript only truly provides dynamic typing - running the code to see what happen. - use a static type system to make predictions about what the code is expected to do before it runs
  • Static types systems describe the shapes and behaviors of what our values will be, when we run our programs

Non-exception Failures, ์˜ˆ์™ธ๊ฐ€ ์•„๋‹Œ ์‹คํ–‰ ์‹คํŒจ

  • ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜: the JavaScript runtime tells us that it thinks something is nonsensical (ECMAScript ๋ช…์„ธ)
  1. In TypeScript, the following code produces an error about location not being defined
// Javascript์—์„œ ์—†๋Š” property์— ์ ‘๊ทผ ์‹œ, ์—๋Ÿฌ๊ฐ€ ์•„๋‹Œ undefined
const user = {
  name: "Daniel",
  age: 26,
};
user.location; // returns undefined
Property 'location' does not exist on type '{ name: string; age: number; }'.
  1. TypeScript catches bugs
// - typo
announcement.toLocalLowerCase(); // toLocaleLowerCase

// - Meant to be Math.random()
Math.random < 0.5;

// - basic logic errors.
// This comparison appears to be unintentional because the types '"a"' and '"b"' have no overlap.
const value = Math.random() < 0.5 ? "a" : "b";
if (value !== "a") {
  // ...
} else if (value === "b") {
This comparison appears to be unintentional because the types '"a"' and '"b"' have no overlap.
  // ๐Ÿšง Oops, unreachable
}

Types for Tooling, ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„๊ตฌ๋กœ์„œ์˜ ํƒ€์ž…

  • the core type-checker can provide error messages and code completion as you type in the editor. (code completion ์ฝ”๋“œ ์™„์„ฑ ๊ธฐ๋Šฅ)
  • An editor that supports TypeScript can deliver โ€œquick fixesโ€ to automatically fix errors, refactorings to easily re-organize code, and useful navigation features for jumping to definitions of a variable, or finding all references to a given variable. All of this is built on top of the type-checker and is fully cross-platform

tsc the TypeScript compiler

tsc hello.ts
  • tsc
    • ts to js, compile or transform
  • ์‚ฌ๋žŒ์ด ์ž‘์„ฑํ•œ ๋“ฏ์ด ๊น”๋”ํ•˜๊ณ  ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
    • ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰
    • ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜๋Š” ๊ฒƒ์„ ๊ฐ์•ˆ
    • ์ฝ”๋“œ ์ฃผ๋ณ€์— ์ž‘์„ฑ๋œ ์ฃผ์„๋„ ์ž˜ ๋ฐฐ์น˜
  • js ์ฝ”๋“œ๋งŒ ์ž‘์„ฑํ•ด๋„, TypeScript compiler ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ํ†ตํ•ด command line์—์„œ ๋ฌธ์ œ์ ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Emitting with Errors, ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ

  • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ด๋„ ์ฝ”๋“œ ์‹คํ–‰ ์ค‘๋‹จx, ๋ฐฉํ•ดx
  • TypeScriptโ€™s core values: much of the time, you will know better than TypeScript
    • JavaScript๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ TypeScript๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๋Š” ๊ณผ์ •์—์„œ ํƒ€์ž… ๊ฒ€์‚ฌ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋– ์˜ฌ๋ ค๋ณด์„ธ์š”. ๊ฒฐ๊ตญ์—๋Š” ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ํ†ต๊ณผํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด๋‚˜๊ฐ€๊ฒ ์ง€๋งŒ, ์‚ฌ์‹ค ์›๋ณธ JavaScript ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์ œ๋Œ€๋กœ ์ž˜ ์ž‘๋™ํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ
  • TypeScript๊ฐ€ ๋ณด๋‹ค ์—„๊ฒฉํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ๋ฅผ ์›ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ --noEmitOnError ์ปดํŒŒ์ผ๋Ÿฌ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. hello.ts ํŒŒ์ผ์„ ์ˆ˜์ •ํ•œ ๋’ค ์œ„ ํ”Œ๋ž˜๊ทธ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ tsc๋ฅผ ์‹คํ–‰ํ•ด๋ณด์„ธ์š”.
tsc --noEmitOnError hello.ts

Explicit Types, ๋ช…์‹œ์  ํƒ€์ž…

// type annotation
// greet takes a person of type string, and a date of type Date
function greet(person: string, date: Date) {
  console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}
  • In many cases, TypeScript can infer (or โ€œfigure outโ€) the types for us.

Erased Types

// compiled js
"use strict";
function greet(person, date) {
  // no longer have type annotations.
  console.log(
    // `Hello ${person}, today is ${date.toDateString()}!`;
    "Hello ".concat(person, ", today is ").concat(date.toDateString(), "!")
    // that string that used backticks (the ` character) - was converted to plain strings with concatenations.
  );
}
greet("Maddison", new Date());
  • there really arenโ€™t any browsers or other runtimes that can just run TypeScript unmodified -> That's why TypeScript needs a compiler
Remember: Type annotations never change the runtime behavior of your program.

Downleveling, ๋‹ค์šด๋ ˆ๋ฒจ๋ง

  • TypeScript has the ability to rewrite code from newer versions to older versions
  • By default TypeScript targets ES3

    While the default target is ES3, the great majority of current browsers support ES2015. Most developers can therefore safely specify ES2015 or above as a target, unless compatibility with certain ancient browsers is important.

Strictness, ์—„๊ฒฉ๋„

  • Different users come to TypeScript looking for different things in a type-checker
    • more loose opt-in experience which can help validate only some parts of their program, and still have decent tooling
    • (๋Œ€๋‹ค์ˆ˜์˜ ์‚ฌ์šฉ์ž๋“ค) prefer to have TypeScript validate as much as it can straight away, and thatโ€™s why the language provides strictness settings as well
  • type-checking strictness flags: ์ผœ๊ณ  ๋Œ ์ˆ˜ ์žˆ๋Š” ํƒ€์ž… ๊ฒ€์‚ฌ ์—„๊ฒฉ๋„ ํ”Œ๋ž˜๊ทธ
    • CLI์—์„œ --strict ํ”Œ๋ž˜๊ทธ
    • tsconfig.json์— "strict": true
    • ๊ฐœ๋ณ„์  ์˜ต์…˜: noImplicitAny, strictNullChecks

noImplicitAny

  • using any often defeats the purpose of using TypeScript in the first place. The more typed your program is, the more validation and tooling youโ€™ll get

strictNullChecks

  • makes handling null and undefined more explicit

English
  • alluding: /ษ™หˆloอžod/, suggest or call attention to indirectly; hint at. ์•”์‹œ
  • particular: /pษ™(r)หˆtikyษ™lษ™r/
  • explicit: /ikหˆsplisษ™t/, stated clearly and in detail, leaving no room for confusion or doubt.
  • legitimate: /lษ™หˆjidษ™mษ™t/
  • acquaint: /ษ™หˆkwฤnt/, make someone aware of or familiar with. ์ต์ˆ™
  • +, concatenations: /kษ™nหŒkadษ™หˆnฤSH(ษ™)n/ ์—ฐ๊ฒฐ
  • pedantic: /pษ™หˆdan(t)ik/ ๊น๊น, ์—„๊ฒฉ
  • lenient: /หˆlฤ“nyษ™nt/ ๋Š์Šจ