티스토리 뷰
728x90
반응형
타입스크립트의 버전이 올라가면 갈수록 타입 추론이 좋아집니다
타입스크립트 5.5 미만에서는 filter를 통해 undefined를 걸러냈더라도
undefined가 걸러졌는지 추론을 하지 못합니다
function makeBirdCalls(countries: string[]) {
// birds: (Bird | undefined)[]
const birds = countries
.map(country => nationalBirds.get(country))
.filter(bird => bird !== undefined);
for (const bird of birds) {
bird.sing(); // error: 'bird' is possibly 'undefined'.
}
}
타입스크립트 5.5 부터는 filter를 통해 걸러진 값들을 정확히 추론합니다
function makeBirdCalls(countries: string[]) {
// birds: Bird[]
const birds = countries
.map(country => nationalBirds.get(country))
.filter(bird => bird !== undefined);
for (const bird of birds) {
bird.sing(); // ok!
}
}
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-5.html
Documentation - TypeScript 5.5
TypeScript 5.5 Release Notes
www.typescriptlang.org
타입스크립트가 왜 이걸 추론하지 못하는거지 하고 한참을 헤매고 있었는데 4버전을 쓰고 있었더라구요 띠용
728x90
반응형
'javascript' 카테고리의 다른 글
typescript의 다형성과 같은 타입 좁히기(narrowing) (1) | 2024.06.07 |
---|
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 대학생
- 창업
- 애플
- 앱스토어
- 공모전
- 웹표준
- 안드로이드
- 네이버
- 모바일
- 트위터
- 게임
- android
- CSS
- php
- 어플리케이션
- 아이디어
- 소프트웨어
- Apple
- 벤처
- JavaScript
- 경진대회
- AWS
- 자바스크립트
- 구글
- 스마트폰
- 앱
- 아이폰
- iPhone
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함