- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.1k
 
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 2.6.1
Code
interface EntityType {
  id: number,
  name: string,
}
const ids = [1, 2, 3]
const entities = new Map<number, EntityType>()
let relevantEntities: EntityType[] = []
if (ids) {
  relevantEntities = ids.map(id => entities.get(id)).filter(Boolean)
}Note that I have tried many variants of the argument to filter (x => !!x, x => x !== undefined, etc). I'm only using Boolean here since it works in Flow
Further note that making the following change allows the error checker to proceed, but only through basically lying about the data:
if (ids) {
  // get(id) becomes get(id)!
  relevantEntities = ids.map(id => entities.get(id)!).filter(Boolean)
}
Expected behavior:
Assignment proceeds without error, since any undefined values produced in the map operation are filtered out.
Actual behavior:
error TS2322: Type '(EntityType | undefined)[]' is not assignable to type 'EntityType[]'.
  Type 'EntityType | undefined' is not assignable to type 'EntityType'.
    Type 'undefined' is not assignable to type 'EntityType'.
luketanner
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created