νμ μ€ν¬λ¦½νΈλ‘ λ³μλ ν¨μμ κ°μ μλ°μ€ν¬λ¦½νΈ μ½λμ νμ μ μ μν μ μμ΅λλ€.
β νμ νκΈ°(Type Annotation)
:
λ₯Ό μ΄μ©νμ¬ μλ°μ€ν¬λ¦½νΈ μ½λμ νμ μ μ μνλ λ°©μ
β String
// κΈ°λ³Έ JS μ μΈ
const str = 'hello';
// TS λ¬Έμμ΄ μ μΈ
const str: string = 'hello';
β Number
let num: number = 10;
β Array
let arr: Array<number> = [1,2,3];
let heroes: Array<string> = ['captin','thor','hulk'] // μ«μλ₯Ό λ£μΌλ©΄ μλ¬
let items : number[] = [1,2,3];
β Tuple
- λ°°μ΄μ κΈΈμ΄κ° κ³ μ λκ³ κ° μμμ νμ μ΄ μ§μ λμ΄ μλ λ°°μ΄ νμ μ λλ€.
let address: [string, number] = ['gangnam', 100];
β Object
let obj: object = {};
let person: object = {
name: 'capt',
age: 100
};
let person: {name: string, age: number} = {
name : 'thor',
age : 1000
};
// objectλ‘ μ΄λ£¨μ΄μ§ λ°°μ΄
let todoItems: Todo[];
μμ
function fetchTodoItems(): object[] { // objectλ‘ μ΄λ£¨μ΄μ§ λ°°μ΄
const todos = [
{ id: 1, title: 'μλ
', done: false },
{ id: 2, title: 'νμ
', done: false },
{ id: 3, title: 'μ€ν¬λ¦½νΈ', done: false },
];
return todos;
}
// μμ μ½λλ₯Ό μλμ κ°μ΄ κ°λ₯ (μμΈν μ§μ )
function fetchTodoItems(): { id: number; title: string; done: boolean }[] {
const todos = [
{ id: 1, title: 'μλ
', done: false },
{ id: 2, title: 'νμ
', done: false },
{ id: 3, title: 'μ€ν¬λ¦½νΈ', done: false },
];
return todos;
}
β Boolean
let show: boolean = true;
β Any
- λ¨μ΄ μλ―Έ κ·Έλλ‘ λͺ¨λ νμ μ λν΄μ νμ© ν©λλ€.
let str: any = 'hi';
let num: any = 10;
let arr: any = ['a', 2, true];
β Void
- λ°νκ°μ΄ μμ΅λλ€. ( 리ν΄κ°λ€μ΄ μλ κ²½μ° )
function deleteTodo(index: number): void {
todoItems.splice(index, 1);
}
function log(): void {
console.log(todoItems);
}
μ°Έκ³
'π Front-End > TypeScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Union Type ( μ λμ¨ νμ ) κ³Ό Intersection Type (0) | 2022.10.22 |
---|---|
Type Aliases ( νμ λ³μΉ ) (0) | 2022.10.22 |
Interface ( μΈν°νμ΄μ€ ) (0) | 2022.10.22 |
νμ μ€ν¬λ¦½νΈμ ν¨μ (0) | 2022.10.22 |
TypeScript (0) | 2022.10.22 |