JS | TS |
๐งบ ⇒ ๐, ๐ฅฆ, ๐, ๐ | ๐งบ ๐ ⇒ ๐ ๐งบ ๐ฅฆ ⇒ ๐ฅฆ ๐งบ ๐, ๐ ⇒ ๐, ๐ |
JavaScript
- Dynamic Typing
- ์ ์ฐ์ฑ๊ณผ ์์ ๋๊ฐ ๋์
- ๊ท๋ชจ๊ฐ ์ปค์ง๊ณ ์ฝ๋๊ฐ ๊ธธ์ด์ง์ ๋ฐ๋ผ ๋จ์ ์ด๋จ
console.log('1' * 1) // 1
console.log(1 +'') // '1'
TypeScript
- JavaScript์ ์์ ์งํฉ์ผ๋ก JavaScript์ ๋ชจ๋ ๊ธฐ๋ฅ์ด ์์
- JavaScript์ ๋นํด ์๊ฒฉํ ๊ฒ์ฌํจ
- ์์ธํ ์๋ฌ ๋ฉ์ธ์ง ์ ๊ณต
- ์คํ ๊ต์
- ๋ธ๋ผ์ฐ์ ๋ TypeScript๋ฅผ ์ดํดํ์ง ๋ชปํจ. ์ปดํ์ผ๋ฌ๋ฅผ ์ฌ์ฉํ์ฌ .ts(TypeScript)ํ์ผ์ .js(JavaScript) ํ์ผ๋ก ๋ณํ
- ์ฌ์ด ํธํ, ํตํฉ
- ํด๋์ค ๊ธฐ๋ฐ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์์
/* ์๋ฃํ์ด ๋ค๋ฅผ ๋ */
console.log('1' * 1)
// The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
const test = "1"
test = 1
// Cannot assign to 'test' because it is a constant.
- ๋ณ์ ์ ์ธ
let test : string = 'dan';
let test2 : string = 1; // Type 'number' is not assignable to type 'string'.
// Array
let arr : [] = [1 ,2 ,3]; //Type '[number, number, number]' is not assignable to type '[]'. Source has 3 element(s) but target allows only 0.
let arr : number[] = [1 ,2 ,3];
// Object
let tesbObj : { first : string } = { first : 'abc' }
let tesbObj2 : { first : string } = { } // Property 'first' is missing in type '{}' but required in type '{ first: string; }'.
let tesbObj3 : { first?: string } = { }
// Union type : ์ฌ๋ฌ ์๋ฃํ ์ง์ ๊ฐ๋ฅ
let test : string | number = 'dan';
test = 123
- Type alias : ํ์ ์์ฒด๋ฅผ ๋ณ์์ ๋ฃ์ด ์ฌ์ฉ ๊ฐ๋ฅ
type test = string | number ;
let num : test = 123
๋ฐ์ํ
๋๊ธ