interface Type1 {
attr1: string;
attr2: string;
}
interface Type2 {
attr1: string;
attr2: string;
attr3: string; // extra attribute
}
function fn(config: Type1 | Type2): void {
// Property 'attr3' does not exist on type 'Type1 | Type2'.ts(2339)
const { attr1, attr2, attr3 } = config;
console.log(attr1);
console.log(attr2);
console.log(attr3);
}
Error code show before. And I know there is a solution that add optional attribute in attr3. But as far as I'm concerned this solution is not good. Because as matter of fact there only exist 2 situations either Type1 or Type2. In a word, optional way is not readability. How can I fix it in advanced way?
attr3, so what value do you expect when trying to get a non-existing attribute?Type1and makeattr3optional or catchts.com/unions#safe_union