Computer Science/TypeScript
-
[TypeScript] Declare 의 용도Computer Science/TypeScript 2023. 11. 19. 23:03
TypeScript에서 Declare 키워드는 주로 외부 라이브러리나 모듈을 정의할 때 사용된다. Declare의 용도는 함수, 클래스 등이 어딘가에 이미 선언이 되었다고 TypeScript 컴파일러에 알리는 용도이다. 즉 TypeScript로 작성되지 않은 코드에서 타입 정보를 주고 싶을 때 사용된다. 따라서 JS 로 변환될때 전부 사라진다. 이러한 이유로 Type 같은 경우에 어차피 JS 코드로 변환되지 않으므로 declare 키워드를 사용하지 않아도 된다. //index.ts type testType = { name: string; age: number; } const test: testType = { name: 'test', age: 10 } ///////////////////////// //in..
-
Utility Types ( Partial, Required, Pick&Omit )Computer Science/TypeScript 2023. 10. 21. 22:28
1. Utility Type이란 2. Partial 3. Required 4. Pick & Omit 1. Utility Type 이란 타입스크립트에서 기존에 존재하는 타입을 변환하여 새로운 타입을 만들어내는 데 사용되는 일련의 제네릭 타입. Utility Type을 통해 더 유연하게 타입을 조작하고 제공할 수 있다. 2. Partial 객체의 모든 속성을 옵셔널 타입으로 만들어준다. 주로 API에서 받아온 데이터의 형태가 일정하지 않거나, 일부 데이터만 필요할 때 사용된다. interface Product { id: number; name: string; price: number; description?: string; } function updateProduct(product: Partial) { } ..
-
Template Literal TypeComputer Science/TypeScript 2023. 10. 14. 15:54
1. Template Literal Type 2. Base 형태 3. Uniton Type을 이용한 Template Literal Type 4. 여러개의 Union Type을 이용한 Template Literal Type 5. Conditional Type을 같이 사용하여 추론하기 6. 공백 제거 및 특정 형태 쪼개기 7. 기타 8. 참조 1. Template Literal Type이란? String Literal Type을 기반으로 새로운 타입을 만드는 도구 2. Base 형태 type Champion = 'Malphite'; type ChampionLane = `${Champion} Top`; 위와 같이 한다면 string으로 Malphite Top을 만들 수 있는 Type을 생성한다. 3. Unio..
-
inferComputer Science/TypeScript 2023. 9. 30. 22:40
infer는 조건부 타입에서만 사용이 가능하다. Conditional types ( 조건부 타입 ) 조건부타입은 TypeScript에서 특정 조건에 따라 타입을 결정하는 기능이다. T extends U ? X : Y 형태를 가지며 이는 타입 T가 타입 U에 할당 가능하면 X 타입을 반환하고, 그렇지 않으면 Y 타입을 반환한다는 뜻이다. type IsString = T extends string ? "yes" : "no"; type Test1 = IsString; // "yes" type Test2 = IsString; // "no" infer 조건부 타입 내에서 동적으로 타입을 추론할 때 사용한다 type ReturnTypeOf = T extends (...args: any[]) => infer R ? ..
-
type vs interfaceComputer Science/TypeScript 2023. 9. 22. 22:57
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces Documentation - Everyday Types The language primitives. www.typescriptlang.org TypeScript 공식 사이트에서 간략하게 차이를 설명하고 있다. 1. 선언 Interface 와 type 모두 타입을 정의하는 주요 방법이지만, 선언 방식과 정의 할 수 있는 경우의 차이가 존재한다. 둘다 객체와 함수를 정의할 수 있지만 string, number, union, tuple 등 기본 타입은 type 에서만 선언 할 수 있으며 interface에서..
-
TSC vs BabelComputer Science/TypeScript 2023. 9. 15. 16:18
1. TypeScript 란 2. tsc 란 3. Babel 이란 4. tsc vs babel 5. tsc 사용법 6. babel 사용법 TypeScript란 TypeScript는 JavaScript의 Superset으로 기존 JavaScript문법에서 정적 타입 시스템을 추가한 것으로 코드의 안정성과 가독성을 향상시킨다. 다면 결과적으로 실행되는 코드는 JavaScript이기에 TypeScript 문법에서 JavaScript문법으로 변환되는 과정이 필요하다. 이 과정에서 크게 2가지가 사용 되는데 tsc와 @babel/preset-typescript 이다. TSC 란 tsc는 TypeScript를 JavaScript로 변환시키는 명령어로 TypeScript Compiler 라고 할 수 있다. 현재 ts..