Skip to content

Instantly share code, notes, and snippets.

@cawoodm
Last active May 11, 2022 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cawoodm/aea81c452d1d74a5acdc7eb294d7a7ed to your computer and use it in GitHub Desktop.
Save cawoodm/aea81c452d1d74a5acdc7eb294d7a7ed to your computer and use it in GitHub Desktop.
Example of TypeScript AS Compiler Error
interface MyObj1 {
[key: string]: string; // All other properties must be strings
name: string; // Mandatory properties
}
interface MyObj2 {
name: string; // Mandatory properties
age: number
}
let obj1: MyObj1 = {
name: 'foo',
'joe': 'mo',
};
let obj2: MyObj2 = {
name: 'phil',
age: 2,
};
console.log('obj1', obj1);
// Here a compiler error:
console.log('obj2', (obj2 as MyObj1).name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment