TIL( TypeScriptํ•™์Šต )

2024. 6. 27. 23:23ใ†TIL

๐Ÿ’ก์ธํ”„๋Ÿฐ ๊ฐ•์˜์‹œ์ž‘ : ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์˜ฌ์ธ์› (๋ฌธ๋ฒ•ํŽธ)

 

1. ๊ธฐ๋ณธ ํƒ€์ž… ์ง€์ •

const a = '5'; //  const๋Š” ๊ฐ’์ด ์ง€์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด :string์œผ๋กœ ํƒ€์ž…์„ ํ•œ๋ฒˆ๋” ์ง€์ •ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค.
const b = 5;  // const b:number = 5;
const c = undefined; // const c:undefined = undefinded
const d = true;  // const d:boolean = true;
const e = null;   // const e:null = null;

 

2. ํƒ€์ž…์— ๊ณ ์ •๋œ ๊ฐ’ ์ง€์ •

//ํƒ€์ž…์— ๊ณ ์ •๋œ ๊ฐ’ ์ง€์ • ๊ฐ€๋Šฅ (ํƒ€์ž…์€ ํ•ญ์ƒ ์ •ํ™•ํ•ด์•ผํ•œ๋‹ค.)
const f: true = true;
const g: 2 = 2;

 

3. ํ•จ์ˆ˜ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

function add(x:number, y:number): number {return x + y};
const result = add( 1,5)


const add: (x:number, y:number) => number = (x, y) => x + y;

 interface Add {
     (x:number, y: number): number;
 }
 const add: Add = (x, y) => x + y;

 

4.๋ฐฐ์—ด ๊ณผ ํŠœํ”Œ ํƒ€์ž… ์ง€์ •

const arr: string[] = ['123','345']
const arr2: number[] = [123, 234]
const arr3: Array<number> = [123, 345] // < > => ์ œ๋„ค๋ฆญ์ด๋ผ๊ณ  ํ•œ๋‹ค.
const arr4: [number, number, string] = [123, 566, '456'] //ํŠœํ”Œ์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐฏ์ˆ˜๊นŒ์ง€ ์ง€์ •๋จ

 

5. ๊ฐ์ฒด ํƒ€์ž… ์ง€์ •

const obj: { lat: number, lon: number} = {lat: 37.5, lon: 127.5}
====
const obj = {lat: 37.5, lon: 127.5}

 

 

** ์ฝ์–ด๋ณผ ๋ฌธ์„œ

TypeScript: Handbook - The TypeScript Handbook (typescriptlang.org)

 

Handbook - The TypeScript Handbook

Your first step to learn TypeScript

www.typescriptlang.org

TypeScript: Documentation - TypeScript 5.5 (typescriptlang.org)

 

Documentation - TypeScript 5.5

TypeScript 5.5 Release Notes

www.typescriptlang.org

 

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL ( ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ - ts )  (0) 2024.07.01
TIL ( prisma ์—†์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(MySQL) ์—ฐ๊ฒฐํ•˜๊ธฐ )  (0) 2024.06.28
TIL ( TypeScript ํ•™์Šต)  (0) 2024.06.26
TIL ( WebSocket )  (0) 2024.06.25
TIL (class)  (0) 2024.06.24