본문 바로가기

부트캠프교육중/TypeScript22

[TS] interface에 제네릭을 선언하는 경우 그냥 interface를 사용하는 경우 interface Dropdown { value: string; selected: boolean; } const obj: Dropdown = { value: 'abc', selected: false }; interface에 제네릭을 선언한 경우 interface Dropdown { value: T; selected: boolean; } const obj: Dropdown = { value: 'abc', selected: false }; const obj: Dropdown = { value: 124, selected: false }; 그래서 interface Email { value: string; selected: boolean; } interface Product.. 2023. 8. 16.
[TS] 제네릭이란 function logText(text: T):T { console.log(text); return text; } logText('하이'); text의 타입은 하이라는 문자열이다. 반환하는값도 문자열이된다. function logText(text){ console.log(text); return text; } 저기서의 T는 logText라는 함수에서 T라는 타입을 받을거야 라는 의미이다. function logText(text: T){ console.log(text); return text; } 그걸 먼저 파라미터의 타입으로 쓰겠다. function logText(text: T): T{ console.log(text); return text; } 그리고 그걸 리턴할때도 쓰겠다. function logTe.. 2023. 8. 16.
[TS] class 자바스크립트에서 class Person { // 클래스 로직 constructor() { this.name = name; this.age = age; } } 타입스크립트에서 class Person2 { name: string; age: number; constructor(name: string, age: number){ this.name = name; this.age = age; } } 변수의 접근범위도 지정. (private, public, readonly) class Person2 { private name: string: // class안에서만 쓰겠다 하면 private추가 가능 (변수의 접근범위) public age: number; //그렇지 않으면 기본적으로 public들어간다. readonly.. 2023. 8. 15.
[TS] enum enum(이넘) -특정 값들의 집합을 의미하는 자료형 숫자형 이넘 enum Shoes { Nike, Adidas } let myShoes = Shoes.Nike; console.log(myShoes); //0 별도의 숫자를 할당하지 않으면 숫자형 이넘으로 취급을 해서 Shoes.Nike = 0; 이렇게 된다. 그래서 결과적으로 콘솔에 0 찍힌다. Adidas = 1; 만약 Nike =10;이렇게 할당하면 Adidas는 11이 된다. 문자형 이넘 enum Shoes { Nike = '나이키', Adidas = '아디다스' } let myShoes = Shoes.Nike; console.log(myShoes); // '나이키' 예제 enum Answer { Yes = 'Y', No = 'N', } funct.. 2023. 8. 15.