Class typescript를 쓰는 이유라고 할 수 있는 class이다. javascript에서도 class문법이 이미 있긴 하지만 우리가 원하는 객체지향 프로그래밍 언어 수준까지는 지원해주지 않기 때문에 javascript에서의 class는 그저 좀 더 구체적인 객체처럼 다루기 위해서 쓰는 것에 불과했기 때문이다. Field class Point ...
[Typescript] 09. 타입 확장
Extending Types class가 상속이 되듯이, type도 상속이 가능하다. type BasicAddress = { name?: string; street: string; city: string; country: string; postalCode: string; } interface AddressWithUnit exten...
[Typescript] 08. 인덱스 서명
Index Signature 아주 가끔 객체 안에 들어있는 속성들의 key를 특정짓지 못하는 경우가 있을 수 있다. 그런 경우 객체 속성의 key를 모르는 상태이기 때문에 Type 정의에도 지장이 생긴다. 이럴 경우에는 인덱스 서명 방식을 사용할 수 있다. type Props = { [key: number]: string | boolean | Fu...
[Typescript] 07. 읽기 전용
Readonly Javascript에서 객체를 구성하는 수많은 Attributes는 Writable하면서 Readable하다. 따지고 보면 수정하면 안되는 Attribute마저도 수정이 가능하다. const test = 'good'; console.log(test.length); // 4; test.length = 777; // success cons...
[Typescript] 06. 오버로딩
Function Overloading 다른 언어를 써 본 경험이 있다면 Overloading가 뭔지 알 것이다. 하지만 Javascript가 시작 언어고, 다른 언어를 전혀 써본 경험이 없다면 생소할 수 있는데, 쉽게 말해서 동일한 함수에 넘기는 Parameter로 실행 로직을 달리하는 방법이라고 보면 된다. 만약에 매개변수로 a b c라는 numb...
[Typescript] 05. 필수가 아닌 파라미터
Optional Parameters Javascript에서는 함수의 선언부에서 표기 된 Parameters 갯수를 전부 충족시키지 않은 실행이 이루어져도 에러가 나지 않는다. const consolePrint = (a, b, c) => { console.log(a, b, c); }; consolePrint(1, 2); // 1, 2, unde...
[Typescript] 04. 제네릭
Generic Function 만약 당신이 Java를 익숙하게 사용하는 개발자라면 이 섹션은 이해하기 정말 쉬울 것이다. 다른 언어에서 흔히들 Generic이라고 부르는 것과 동일하기 때문이다. 예를 들어, 당신이 Stack 자료구조를 직접 구현한다고 해보자. Stack에는 정말로 많은 Type의 데이터가 들어간다. Number String Boole...
[Typescript] 03. 시그니처
type DescribableFunction = { description: string; (someArg: number): boolean; }; function doSomething(fn: DescribableFunction) { console.log(fn.description + " returned " + fn(6)); } 위 예제는 처...