- 타입스크립트는 자바스크립트의 변수, 매개변수, 리턴값에 type을 붙여놓은 것! 주의할 점: 타입을 대문자로 쓰면 안된다!
tip) type(:뒤에 있는 내용)을 지우면 js 코드가 되도록 만들어보자!
e.g.
const a: number = 5;
const b: string = 'hello';
const c: boolean = true;
const d: undefined = undefined;
const e: null = null;
const f: symbol = Symbol.for('abc');
const g: bigint = 1000000n;
const f: any = true;
function add(x: number, y: number): number {return x + y}
function add(x: number, y: number): number;
function add(x, y) {
return x + y;
}
const add: (x: number, y: number) => number = (x, y) => x + y;
type Add = (x: number, y: number) => number;
const add: Add = (x, y) => x + y;
interface Add {
(x: number, y: number): number;
}
const add: Add = (x, y) => x + y;
const obj: { lat: number, lon: number } = { lat: 37.5, lon: 127.5 }
const arr: string[] = ['123', '456']
const arr2: number[] = [123, 456]
const arr3: Array<number> = [123, 456]
const arr4: [number, number, string] = [123, 456, 'hi']
const f: true = true;
const g: 5 = 5;
let aa = 123;
aa = 'hello' as unknown as number;
let arr = [];
let arr: string = [];