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 ๋ช ์ธ)
- 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; }'.
- 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/ ๋์จ