TypeScript Interface only highlights single problem, not multiple

Viewed 37

This seems like a TypeScript bug, but I'm not 100% sure.

If you see the example below, I'm expecting two errors (in gameData, highlighted by comments) but TypeScript intellisense will only highlight one of them.

If you comment out either of them, the other will error, but never both of them.

Does anyone know if there's a reason for this? Or if it's a bug?

Demo on TypeScript playground

interface Saved {
  highScore?: number;
  levelsComplete?: number;
}

interface GameData {
  score: number;
  timeTaken?: number; 
  saved?: Saved; 
}

const gameData: GameData = {
  score: 0,
  timeTaken: 60,
  saved: [1, 2, 3], // this should error as it does not match Saved - if this is commented out, 'averageScore' now errors
  averageScore: 10, // this property doesn't exist on GameData and should also error
};
1 Answers

I'm not a typescript developer and have never contributed to typescript, but looking at the source code, I would not call it a bug per se, rather an implementation detail. Searching for has_no_properties_in_common error message in TS repository reveals that it is used in this file on line 17861 inside the isRelatedTo function. Now Object_literal_may_only_specify_known_properties error is found in this same file on line 18095 inside the hasExcessProperties function.

This suggests, that when checking for assignability, typescript first checks whether the types are related, which includes comparing their properties' types, then looks for the excess ones. Maybe one day this will be fixed, but it seems like it won't be very easy.

Related