Javascript/TypeScript 13

[Typescript] 타입스크립트로 작성되지 않은 패키지 import할때 해결방법

패키지를 import했는데 Cannot find module ' ' or its corresponding type declarations. 해결방법 GitHub - DefinitelyTyped/DefinitelyTyped: The repository for high quality TypeScript type definitions. GitHub - DefinitelyTyped/DefinitelyTyped: The repository for high quality TypeScript type definitions. The repository for high quality TypeScript type definitions. - DefinitelyTyped/DefinitelyTyped github.com 사이트..

[Typescript] js 소스코드를 typescript처럼 사용하기 (JSDOC)

js 소스코드에 주석으로 @ts-check 를 넣으면 typescript 처럼 소스코드를 체크해줌 // @ts-check vscode에서 /** 하고 엔터를 하면 자동완성이 되는데 파라미터와 return 타입을 정할 수 있다. /** * Initializes the project * @param {object} config * @param {boolean} config.debug * @param {string} config.url * @returns boolean */ export function init(config) { return true; } init 함수를 사용할 때 설명을 볼 수 있다.

[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(..