더북(TheBook)

▼ 표 2-1 타입으로 사용 가능한 값

분류

설명

예시

기본형

string, number, boolean, symbol, object, undefined, null, bigint

const a: string = 'hello';

const b: object = { hello: 'ts' };

배열

타입 뒤에 []를 붙임. 길이가 고정된 배열이면 [] 안에 타입을 적음

const a: string[] = ['hello', 'ts', 'js'];

const b: [number, string] = [123, 'node'];

상수

1, 'hello', true 등의 고정값

const a: 1 = 1;

변수

typeof 변수로 해당 변수의 타입 사용 가능

let a = 'hello';

const b: typeof a = 'ts';

클래스

클래스 이름을 그대로 인스턴스의 타입으로 사용 가능

class a {}

const a = new A();

type 선언

다른 타입들을 조합해서 새로운 타입 생성 가능. 유니언(|), 인터섹션(&) 사용 가능

const a: { hello: string } = { hello: 'ts' };

type B = { wow: number };

const b: B = { wow: 123 };

type C = string | number;

let c: C = 123;

인터페이스

interface 선언

interface a { hello: string, wow: number }

const a: A = { hello: 'ts', wow: 123 }