전체 글 55

[TypeScript] 추상 메서드 vs 인터페이스 차이 (abstract vs interface)

Typescipt에서의 차이 추상메서드의 예시는 아래와 같다 abstract class Test { constructor( protected nickname: string, protected level: number ) { } abstract attack(): void abstract guard(): void } class Player extends Test { attack(): void { console.log("Attack"); } guard(): void { console.log("shiled") } } 다음은 인터페이스 예시이다. interface Test2 { nickname:string, level:number, attack():void, guard():void } class Player2 i..

[TypeScript] 제네릭(Generics) 사용하는 이유

제네릭 사용하는 이유 type Test = { (arr : number[]) : void; (arr : string[]) : void; (arr : boolean[]) : void } const test1 : Test =(arr) => { arr.forEach(a => console.log(a)) } test1([1,2,3,4]) test1(["a","b","c"]) test1([true,false,true]) test1([1,"a",true,"b"]) test1에 새로운 종류의 배열을 넣을때 마다 test타입에 계속 내용을 추가해야는 불편함이 있음 15번째 줄처럼 여러 타입이 오면 처리하기 힘듬 문법 type Test2 = { (arr:T[]) : T } const test2 : Test2 = (arr..

[TypeScript] 오버로딩 (Overloading)

오버로딩(Overloading) 동일한 이름의 함수에 파라미터 개수나, 타입을 다르게 하여 함수를 만드는 것 매개변수의 개수가 같고 타입이 다를 때 type Test1 = { (a:number, b:number) : void (a:string, b:number) : void } const test1:Test1 = (a,b) =>{ if(typeof a ==="string") console.log(a) else return console.log(a+b) } 매개변수의 개수가 다를 때 type Test2 = { (a:number, b:number) : void (a:number, b:number, c:number) : void } const test2:Test2 = (a,b,c?:number) =>{ if(..

[TypeScript] 타입 별칭(Type Aliases), 선택적 타입

타입 별칭(Type Aliases) const name : string = "nick" //타입별칭 사용시 type Name = string; const name : Name = "nick"; //interface 같은 복잡한 타입도 가능 type Customer = { name : string, phone : number } //interface를 만들때는 "=" 이 없음 interface Customer { name : string; age? : number; phone : number; } //제네릭도 가능 type User = { name : T } optional type(선택적 타입) 변수옆에 ?를 붙이면 선택적 타입이 된다 선택적 타입은 명시한 타입 | undefined ex) age의 타입..