TIL_72_230209

young0_0·2023년 2월 15일
0

TIL

목록 보기
69/91

72일 차 회고

  • typescript interface vs type

interface vs type

interface

  • 선언병합 : interface는 이름을 똑같이 선언했을 때 자동확장 가능
  • 주로 객체의 타입을 설정 할때 (부모-자식)
interface Person {
	name : string
	age: number
}
//확장
interface Student extends Person {
	schoo; : string
}

//선언병합
interface Person {
	old:string
}

//객체
let a : person = {
	"name" : "타입스크립트"
	"type" : "typescript" 
}

type

  • 기존에 존재하는 타입을 이름만 바꿔 사용 가능
  • 객체가 아닌 primitive(원시적인) type 일때
  • 병렬적인 관계 → type (union)
  • 함수반환형에서는 shorthand라서 더 좋으며 사용한다.
//가장 기본적인 형식을 참조한다.
const var_1: string = "";
const var_2 : boolean  = false;
const var_3 : undefined = undefined;
const var_4: Array<any> = []

// union
type CombineNumberString = string | number;

// 함수반환형
type B = (name: string) => string
const b : B = (name) => "hello"

참고

profile
열심히 즐기자ㅏㅏㅏㅏㅏㅏㅏ😎

0개의 댓글