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
반응형