Typescript
-
enum 타입,Typescript 2023. 1. 16. 21:20
enum 타입 - 자바스크립트에는 없고, 타입스크립트에만 있는 타입 - enumeration의 줄임말 - 숫자형 enum enum Direction { Up, // = 0 Down, // = 1 Left, // = 2 Right, // = 3 } console.log(Direction.UP, Direction.Down, Direction.Left) // 결과 : 0 1 2 const up: Direction = Direction.Up; // Direction = Direction.UP | Direction.Down | Direction.Left | Direction.Right const leftOrRight: Direction.Left | Direction.Right = Direction.Left; en..
-
함수 타입Typescript 2023. 1. 16. 18:21
Optional Parameter - 함수의 파라미터가 들어오지 않을 것 같으면 "?"(옵셔널 파라미터)를 사용한다. // JS function add2(n1, n2) { if (!n2) return n1; return n1 + n2; } // TS function add2(n1: number, n2?: number): number { if (!n2) return n1; return n1 + n2; } const a2 = add2(1, 2); const b2 = add2(10, 20, 30); // Error, js라면 30은 undifined가 나올것이다. const c2 = add2(10); function add2(n1?: number, n2: number): number { // 맨 앞자리는 불가..
-
-
Typescript 설치 및 세팅Typescript 2023. 1. 16. 11:05
간단하게 과정만 나열하려고 한다. npm itit -y 프로젝트 초기설정 npm install typescript 타입스크립트 설치 npx tsc --init 타입스크립트 컴파일러 / 사용할 모듈을 로컬에 저장하지 않고, 매번 최신 버전의 파일만을 불러와서 실행 한 후, 해당 파일을 없애는 방식으로 모듈을 사용하는 것 tsconfig.json 파일 확인해보면, target과 module이 있는데, 이 것을 확인하는 이유는, 컴파일을 하게되면 js 파일이 생성되는데, js파일의 버전(?)을 결정하는 부분이다. 브라우저는 Typescript를 이해하지 못해서, 이렇게 컴파일을 통해서 자바스크립트 파일로 변환 해주어야 한다. https://www.typescriptlang.org/play?#code/PTAEH..