Last active
May 11, 2022 13:37
-
-
Save cawoodm/aea81c452d1d74a5acdc7eb294d7a7ed to your computer and use it in GitHub Desktop.
Example of TypeScript AS Compiler Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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