개발일지 (11월 4주차 회고)

2020. 12. 9. 13:27Developer History

 

 

Study History

 

면접과 코딩테스트, 과제등의 여러 준비들로 인해서 추가적인 개발공부에 여력을 쏟을 수 없었던 한 주였다. 주로 타입스크립트 공식 문서를 조금씩 읽었다.

 

 

 

Typescript

타입스크립트는 자바스크립트의 런타임 속성을 변화시키지 않는다. 자바스크립트 코드를 타입스크립트로 변환시키는 것은 타입 오류를 검사하기는 해도 같은 방식으로 실행시킬 것을 보장한다. 이렇게 자바스크립트 자체를 그대로 유지하면서 타입 검사만 추가하는, 말 그대로 레이어만 하나 추가하는 방식으로 작동한다.

 

 

 

타입 추론
타입스크립트 공식 문서에는 interface를 우선적으로 사용하고, 특정 기능이 필요할 때 type을 사용하라고 권장하고 있다.

타입스크립트는 DuckType을 지원하는데 이것이 흔히 사용되는 "타입 추론"을 의미한다.

 

Typescript enforces different levels of strictness depending on what you're doing. When you create a variable with an explicit type, typescript is very strict about what must be there. You must exactly follow the type definition. Anything else is very likely a bug in your code, so by being as strict as possible it helps you to catch bugs.
When you try to assign one variable to another, including passing a variable into a function, typescript does a looser check. The two variables just need to be compatible with eachother, which roughly means they need to have at least the listed properties with the right types, but it's ok if it also has more properties. This looser level of checking is important to support subtypes and subclasses. You should be able to pass a subclass in where a baseclass is called for (the liskov substitution principle), but the subclass will often have extra properties

 

요지는, 명시적인 타입을 설정한 객체의 경우 타입스크립트는 이에 대해 "엄격한" 타입 비교를 수행한다. 즉, 모든 property가 들어있는지, 추가적인 property는 없는지를 엄격하게 검사한다는 것이다. 

 

하지만 특정 변수를 함수의 Arguments로 넘기는 경우에, 타입스크립트는 "덜 엄격한" 타입 비교를 수행한다. 즉, Arguments에 필요한 타입이 다 들어있기만 하다면, 변수에 추가적으로 property가 들어가 있어도 타입 에러를 내뿜지 않는다는 것이다. 그냥 필요한 property만 있으면 된다는 개념이다. 

 

 

Etc

- 기본적으로 null과 undefined는 다른 모든 타입의 하위 타입이다.

- never를 반환하는 함수는 함수의 마지막에 도달할 수 없는 것들이다. 예를 들어 에러를 반환하거나, 무한 루프를 도는 등,  반환하지 않는 함수들에 한해서 never타입이 적용된다.

 

 

 

 

 

Feedback

 

처음으로 StackOverflow에 Typescript 관련 질문을 해보았다.

생각보다 빠르게 답변을 받을 수 있었고 내가 의도하지 않은 대답들 까지 얻을 수 있었는데, 앞으로도 자주자주 Stackoverflow에 질문을 올려야겠구나 생각했다.

 

TypeScript Related Stackoverflow Feedback (Duck Typing)

stackoverflow.com/questions/65226741/typescript-misunderstanding-about-duck-typing

 

Typescript: Misunderstanding about duck typing

I'm studying Typescript reading Handbooks & Docs. And i'm just curious about how Typescript Duck Typing Works. For example, this code provides Type error (using Typescript official Playground)

stackoverflow.com

 

 

반응형

'Developer History' 카테고리의 다른 글

산업기능요원 전직 후기  (5) 2020.12.09
개발일지 (12월 2주차 회고)  (0) 2020.12.09
개발일지 (12월 1주차 회고)  (0) 2020.12.09
개발일지 (11월 3주차 회고)  (1) 2020.11.20
개발일지 (11월 2주차 회고)  (0) 2020.11.16