TypeScript
A javaScript Superset
A Language building up on javaScript
Adds new Features + Advantages to javascript (์๋ก์ด ๊ฒ์ด ์๋๋ค)
Browser can't execute it
Compiled to js
add('1','2')
Unwanted Behavior at Runtime
result 23
Mitigation Stategies
Add if check to add fuction Validate & sanitize user input
this is not a technical error
it's not an error which is thrown but it's a logical error
the value of an input element is always a number
using typescript
compiler error
์ฌ๊ธฐ์ !
๋ ์ ๋ null์ด ์ค์ง ์๋๋ค๋ ๋ป์ด๋ค.
ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ถ๊ฐํ๋๊ฒ์ type๊ณผ data๊ฐ ๋งค์ฐ ์ค์ํ๋ค.
์ฅ์
clean code
์ค๋ฅ ๋ฐฉ์ง(์๊ธฐ์น ์์ ์ํฉ์ ํผํ ์ ์๋ค.)
์ด๋ป๊ฒ ์๋ํ๋์ง ๋ช ์์ ์ผ๋ก ์ง์
์ปดํ์ผํ์ฌ ์ด์ ๋ฒ์ ์ ๋ธ๋ผ์ฐ์ ๋ ์ง์ํ๋ค.(Next-gen JavaScript)
Non-javaScript Features like Interfaces or Generics
Meta-Programming Features like Decorators
Rich Configuration Options
Modern Tooling that helps even in non-TypeScript Projects
lite server
Core Types
number
String
Boolean
Object
Array
Tuple
Enum
Any
number
typeScript's type system only helps you during development.
๋ธ๋ผ์ฐ์ ๋ ์ง์ํด์ฃผ์ง ์๋๋ค.
JavaScript uses "dynamic types" (resolved at runtime), TypeScript uses "static types" (set during development).
Object Types
nickname์ ์ค์ ํ์ง ์์ ์ํ์์ nickname์ ํ์ธํ๊ณ ์ ํ๋ค๋ฉด ๊ฐ๋ฐํ๊ฒฝ์์ ํ์ ์คํฌ๋ฆฝํธ๊ฐ ์ค๋ฅ๋ฅผ ์๋ ค์ค๋ค.
Property 'nickname' does not exist on type '{ name: string; age: number; }'.
6 console.log(person.nickname);
Array
toLocaleUpperCase๋ฅผ ์ผ์๋๋ ๋ฌธ์ ๊ฐ ์๋ค.
map์ Array ๋ฉ์๋๊ธฐ ๋๋ฌธ์ ํ์ ์คํฌ๋ฆฝํธ๊ฐ ์๋ฌ๋ฅผ ๋ฟ๋ฟํด์ค๋ค.
Tuple
Enum
js์์ ์์๊ด๋ฆฌ๋ฅผ ํ ๋๋ const์ ๋๋ฌธ์ ๋ณ์๋ช ์ ์ด์ฉํ์ฌ ํ์๋ค.
enum์ ์ด๊ฑฐํ์ผ๋ก ์ฝ๊ฒ ํ ์ ์๋ค.
typescript๋ก ๋ฐ๊พผ๋ค๋ฉด
enum Role { ADMIN, READ_ONLY, AUTHOR };
์ด๋ ๊ฒ ์ฌ์ฉํ ์ ์๋ค. ๋ฐฐ์ด์ฒ๋ผ 0, 1, 2์ ํด๋น๋๋ค.
๋ณํํ ํ์ผ์์ ํ์ธํ ์ ์๋ค.
์๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ ์ซ์๋ ๋๊ณ ํ ์คํธ๋ ๋๋ค.
any
์ต๋ํ ์ฐ๋ ๊ฒ์ ํผํ์
๋ช ์์ ์ผ๋ก ํ์ ์ ๋ํ๋ด์ง ์๊ณ ์ ์ถ๋ฅผ ํตํ๋ค๋ฉด ํ์ ์คํฌ๋ฆฝํธ ์ฅ์ x
Union Types
|
์ ์ด์ฉํ์ฌ ํ์
์ ์ง์ ํ ์ ์๋ค.
Type Aliases
type์ ์ด์ฉํ์ฌ ์๋จ์ ์ง์ ํ ์ ์๋ค.
Function Return Types and Void
parameter ๋งค๊ฐ๋ณ์ ์์ :
๋ฅผ ์ด์ฉํ์ฌ return๊ฐ์ ํ์
์ ์ ์ํด์ค๋ค.
return๊ฐ์ด ์์ ๋๋, return;๋ง ํ ๋ void๋ก ์ ์ํ๋ค.
undefined ์ธ ๊ฒฝ์ฐ์ ์๋ ๊น?
return;
์ด๋ฐ ์ํฉ์ ๋งํ ์ ์๋ค. ํ์ง๋ง void
๋ก ์ด๋ค.
Function Types
๋ณ์์ ํ์
์ ํจ์๋ก ์ง์ ํ ๋ function์ด๋ผ๊ณ ํ์ง ์๋๋ค. (a: number, b: number) => number;
์ผ๋ก ์ง์ ํ๋ค.
Function Types and Callbacks
์ธ์์ ๋ํ ์ ์๋ ๋งค๊ฐ๋ณ์์์ ํ๋ค. ์์ฌ์ด ๋ค์ด ํ์ธํ๋ ๋ฐฉ๋ฒ์ addํจ์๋ฅผ ํธ์ถ ํ ๋ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ ค๋ณด๋ฉด ํ์ ์ด ์ง์ ๋์ด ์๋ค.
The Unknown Type
์ด๋ ๊ฒ ํ๋ฉด ๋ฐํ์ ์๋ฌ๊ฐ ๋๋ค. unknown๋ก ์ค์ ํ๊ฑด string์ ํ ๋นํ ์ ์๋ค.
๊ทธ๋ ๋ค๋ฉด any๋?
๋ฌธ์ ์๋ค. any๋ก ์ค์ ํ๋ ๊ฒ์ ์๋ฐ์คํฌ๋ฆฝํธ์ฒ๋ผ ์ ์ฐํ๊ฒ ์ฐ๊ฒ ๋ค๋ ๋ป์ด๊ณ ๊ธฐ๋ณธ์ ์ผ๋ก ํ์
๊ฒ์ฌ๋ฅผ ๋นํ์ฑํํ๊ณ ํ์
์คํฌ๋ฆฝํธ๋ฅผ ์ฐ๋ ๊ฒ์ ํฌ๊ธฐํ๊ฒ ๋ค
๋ ๋ป์ด๋ค.
userInput์ ํ ๋นํ๋ ๋ฒ
extra check๋ฅผ ์ถ๊ฐํ๋ค.
The Never Type
ํจ์์์ ์ ๋ return์ด ์์ ๋ ์ฌ์ฉํ๋ค.
generateError์ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ฆฌ๋ฉด void๊ฐ ๋จ์ง๋ง ํจ์ฌ ๋ช ํํ๋ค.
Last updated
Was this helpful?