type allias

Typescript에서는 type도 따로 선언할 수 있다. 길고 복잡한 Union type을 따로 type allias로 만들어 사용하여 가독성 향상시키고 코드 길이를 줄일 수 있다. 

type 변수의 이름은 첫 글자를 대문자로 하는게 국룰

!! type은 재정의가 불가능하다 !!

type Person = { name : string, age : number };
let minseo : Person = { name : Minseo, age : 21 };

 

type extend

type Name = { name : string };
type Age = { age : number };

type Person = Name & Age;

let Minseo : Person = { name : Minseo, age : 21 };

 

function type allias

함수 type allias는 함수 표현식에만 사용할 수 있다.

type FunctionType = ( a : string ) => number; 
// string type의 매개변수를 갖고, return type이 number인 함수 type allias

let myFunction : FunctionType = function(){
	// 함수 표현식
}

object 내의 함수도 함수 type allias를 지정할 수 있다. 

type Member = {
	name : string,
    age : number,
    plusOne : ( x : number ) => number,
    changeName : () => void
}

 

728x90

'Language > Typescript' 카테고리의 다른 글

[ Typescript ] rest parameter type지정  (2) 2021.12.18
[ Typescript ] interface  (0) 2021.12.18
[ Typescript ] class type 지정  (0) 2021.12.18
[ Typescript ] Litertal Types & as const  (2) 2021.12.17
[ Typescript ] Typescript 시작하기  (0) 2021.12.17

+ Recent posts