본문 바로가기
반응형

programming language/TypeScript13

제네릭을 이용한 타입 정의 단순히 유니온 타입을 사용했을 때 각각의 interface 타입정의가 늘어난다. interface Email { value: string; selected: boolean;}const emails: Email[] = [ { value: "naver.com", selected: true }, { value: "gmail.com", selected: false }, { value: "hanmail.net", selected: false },];interface ProdcutNum { value: number; selected: boolean;}const numberOfProducts: ProdcutNum[] = [ { value: 1, selected: true }.. 2022. 12. 15.
ts에서 class를 사용할 때 ts에서 class를 사용할 때 다른점. 1. 클래스에서 사용할 속성(멤버변수)에는 type을 정의해줘야 한다. 2. constructor의 parameter에도 type을 정의해줘야 한다. 3. 멤버변수에는 유효범위도 사용할 수 있다. private, public, readonly등의 속성을 사용할 수 있다. class Person7 { // 멤버변수를 써줘야 한다. name: string age: number // 멤버변수의 유효범위도 쓸 수 있다. private name2: string public age2: number readonly log: string constructor(name:string, age: number){ this.name = name, this.age = age } } 2022. 12. 14.
enum 살펴보기 enum이란? 특정 값들의 집합을 의미하는 자료형으로 어떠한 집합의 데이터 타입이다. enum Shoes{ Nike, Adidas, puma } const myShoes = Shoes.Nike console.log(myShoes) Shoes라는 집단을 만들었다. 해당 데이터를 가지고 변수를 만들었는데 콘솔로 찍으면 0 이 나오게 된다. 이넘을 썼을 때 별도의 값을 지정해주지 않으면 숫자형 enum으로 취급을 하게 된다. 그래서 Shoes.Nike는 첫번 째 값이 0으로 시작된다. // 초기화를 할당하지 않으면 기본값은 0부터 증가 enum Shoes{ Nike = 10, Adidas, puma } const myShoes = Shoes.Adidas console.log(myShoes) //11 Nike .. 2022. 12. 7.
연산자를 이용한 타입 정의_유니온, 인터섹션 union type function logMessage( value:any) { console.log(value) } logMessage('hello') logMessage(100) logMessage(false) any를 사용하면 어떤 타입이건 넣을 수 있다. 하지만 이렇게 사용하면 타입을 사용하는 의미를 잃게 된다. function logMessage( value:string | number) { console.log(value) } logMessage('hello') logMessage(100) || or연산자를 썼을 때의 파이프를 하나 추가해준다. 문자열과 숫자 모두 동일하게 받아서 사용할 수 있게 된다. 장점 타입 가드: 특정 타입으로 타입의 범위를 좁혀나가는 과정(필터링시켜줌) 타입 구분 후 .. 2022. 12. 6.
728x90
반응형