TypeScript Compiler 동작 방식
- Ts Compiler는 TypeScript 소스코드를 AST 로 변환한뒤, 타입 검사를 수행하고 그 후 자바스크립트로 변환하는 과정을 담당한다.
- TypeScript 소스코드를 AST 로 변환하는 과정은 parser.ts / scanner.ts
- 타입검사를 수행하는 과정은 binder.ts, checker.ts
- AST 를 자바스크립트 소스코드로 변환하는 과정은 emitter.ts , transformer.ts 파일이 담당.
checker.ts 파일을 일부를 보면 아래와 같음
/** 함수 매개변수에 전달된 값이 FreshLiteral인 경우 true가 됩니다. */
const isPerformingExcessPropertyChecks =
getObjectFlags(source) & ObjectFlags.FreshLiteral;
if (isPerformingExcessPropertyChecks) {
/** 이 경우 아래 로직이 실행되는데,
* hasExcessProperties() 함수는
* excess property가 있는 경우 에러를 반환하게 됩니다.
* 즉, property가 정확히 일치하는 경우만 허용하는 것으로
* 타입 호환을 허용하지 않는 것과 같은 의미입니다. */
if (hasExcessProperties(source as FreshObjectLiteralType)) {
reportError();
}
}
/**
* FreshLiteral이 아닌 경우 위 분기를 skip하게 되며,
* 타입 호환을 허용하게 됩니다. */
Fresh Literal에만 예외가 있음.