본 게시글은 Do it! 타입스크립트 프로그래밍을 공부한 내용입니다.

 

1. 타입 주석 / 타입 추론 (type annotation / type inference)

let n: number = 1	// type annotation
let m = 2	// type inference

 

2. 인터페이스 (interface)

// 'Person'이라는 인터페이스를 생성. Person은 name을 반드시 갖고, age는 옵션?? 확인 필요.
interface Person {
	name: string
    age?: number
}

// 'person'이라는 변수는 인터페이스에 의해 'Person'타입이다. name을 반드시 정의해야한다.
let person: Person = {name: "Jane"}

 

3. 튜플 (tuple)

let numberArray: number[] = [1, 2, 3]	// array (저장되는 데이터 타입이 모두 같다.)
let tuple: [boolean, number, string] = [true, 1, 'OK']	// tuple (저장되는 데이터 타입이 2가지 이상이다.)

 

4. 제네릭 타입 (generic)

하나의 클래스에 여러 데이터 타입을 사용할 수 있다.

class Container<T> {
	constructor(public value: T) { }
}
let numberContainer: Container<number> = new Container<number>(1)
let stringContainer: Container<string> = new Container<string>('Hello world')
let arrayContainer: Container<number[]> = new Container<number[]>(1, 2, 3)
let booleanContainer: Container<boolean> = new Container<boolean>(true)

 

5. 대수 타입 (ADT, abstract data tyep) : union(=sum type), intersection(=product type)

합집합 : union(=sum type) =>  '|' (OR 기호를 사용)

type NumberOrString = number | string	// 합집합. number or string

 

교집합 : intersection(=product type) => '&' (AND 기호를 사용)

type AnimalAndPerson = Animal & Person	//	합집합. animal and person

 

 

Tag. 타입주석, 타입추론, type annotation, type inference, interface, array, tuple, 제네릭타입, generic, generic type, 대수타입, abstract, abstract data, abstract data type, 합집합, union, sum type, 교집합, intersection, product type, adt, algebraic data type, algebraic

+ Recent posts