Web/TypeScript

[TypeScript] 옵션 / 타입 호환성 / TypeScript Compiler /compileOptions

say! 2022. 10. 6. 11:51
728x90

 

- TypeScript Deep Dive

 

README - TypeScript Deep Dive

TypeScript Compiler Internals

basarat.gitbook.io

 

- 타입스크립트의 타입 시스템

타입을 명시적으로 지정가능

> 명시적으로 타입 지정 안하면, 타입스크립트 컴파일러가 자동으로 타입 추론

 

🔵 Structural Type System vs Nominal Type System

- Structural Type System

구조가 같으면 같은 타입

 

- Nominal Type System (C, JAVA)

구조가 같아도 이름이 다르면 다른 타입

 

- duck typing (Python)

 

🔵 타입 호환성 (Type Compatibility)

- 같거나 서브 타입인 경우 할당 가능 (공변)

- 함수의 매개변수 타입만 같거나 슈퍼타입인 경우 할당 가능 (반병)

 

🔵 타입 별칭 (Type Alias)

Primitve, Union Type, Tuple, Function

직접 작성해야하는 타입의 다른 이름을 지정할 수 있음

만들어진 타입의 refer로 사용하는 것 > 타입을 만드는 것은 아님

Interface와 비슷

 

- tsconfig.json 파일의 schema

http://json.schemastore.org/tsconfig

 

🟣 TypeScript Compiler

- compileOnSave

true / false (default false)

 

 

GitHub - TypeStrong/atom-typescript: The only TypeScript package you will ever need

The only TypeScript package you will ever need. Contribute to TypeStrong/atom-typescript development by creating an account on GitHub.

github.com

 

- extends

상속할 때 사용하는 키워드

파일 상대 경로명 : string

 

- files, include, exclude

내 프로젝트에 어떤 파일을 컴파일할건지 결정

셋 다 설정 없으면 전부 다 컴파일함

* files : 상대 / 절대 경로의 리스트 배열, exclude보다 쎔

* include : glob 패턴 (.gitignore같이), exclude보다 약함, * 같은걸 사용하면 .ts / .tsx / .d.ts만 include (allowJS)

* exclude : glob 패턴 (.gitignore같이), 설정 안하면 (node_modules, bower_components, jspm_packages, <outDir>)를 default로 제외함 / <outDir>은 include에 있어도 항상 제외 

 

🟣 compileOptions

- typeRoots, types

* typeRoots : 배열 안에 들어있는 경로들 아래서만 가져옴

* types : 배열 안의 모듈 혹은 ./node_modules/@types/ 안의 모듈 이름에서 찾아옴, [] 빈 배열 넣는건 이용하지 않겠다는 뜻

typeRoots와 types 같이 사용하지 X

 

- target, lib

어떤 런타임에서 실행할 수 있는지 결정해줌

* target : 빌드의 결과물을 어떤 버전으로 할 것인지 / 지정 안하면 es3

* lib : 기본 type definition 라이브러리를 어떤 것을 사용할 건지 / 지정하면 lib 배열로만 라이브러리 사용함

 

- outDir, outFile, rootDir

 

- strict

type checking option 설정함

* noImplicitAny 옵션

* noImplicitThis 옵션 : 명시적이지 않게 any 타입 사용하여 this 표현식에 사용하면 에러 발생

* strictNullChecks 옵션 : 모든 타입에 자동으로 포함되어 있는 null과 undefined 제거해줌 (예외 : undefined에 void 할당 가능)

* noImplicitReturns 옵션 : 함수 내에서 모든 코드가 값을 리턴하지 않으면 컴파일 에러 발생시킴

* strictFunctionTypes 옵션 : 함수 할당할 때 매개변수 타입이 같거나 슈퍼타입이 아닌 경우 에러를 통해 경고함

* strictPropertyInitialization 옵션 : --strictNullChecks 옵션 설정해야지 사용 가능

* strictBindCallApply 옵션 : bind, call, apply에 대해 더 엄격한 검사 수행

* alwaysStrict 옵션 : 각 소스파일에 대해서 JavaScript의 strict mode로 코드 분석


프로젝트 할 때 strict를 true로 설정하고 사용하기!