티스토리 뷰

javascript

타입스크립트 5.5 이상 써야하는 이유

나를찾는아이 2025. 3. 17. 17:55
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
링크
«   2025/03   »
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
글 보관함