I have a TypeScript interfaces:
export interface iA {
name: string;
}
export interface iB {
size: number;
}
export interface iX {
id: number;
data: iA | iB;
}
Then in one of my Angular component class I'm using interface iX:
// example.component.ts
class ExampleComplnent {
public item: iX = {
id: 1,
data: { name: 'test' }
}
}
<!-- example.component.html -->
<div>{{ item.data.name }}</div>
This will throw an error: Propery 'name' not exist on type 'iB'.
I know that in a TypeScript file I can use casting like this:
const name = (item.data as iA).name;
But how can I make casting on the HTML side?
I also want to mention that in my code I'm validating that item.data is of type iA, not of type iB. So I'm sure it have the property 'name'.
item.dataeither have anameor asize? If so, what do you want to do if the one you render doesn't have aname? Note that's not type casting exactly, it's a type assertion. - jonrsharpe