If we have
interface A { name: string; id: number; } interface B { uni: string; age: number; } type ArrType = Array<A | B> declare var arr; let arr: ArrType; arr.map(item => { // need to detect if item is of type A or B })
How can we do this…?
function isA(v): v is A { return "name" in v && "id" in v; } function isB(v): v is B { return "uni" in v && "age" in v; } arr.map(item => { if(isA(item)) { console.log(item.id, item.name); } })
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If we have
How can we do this…?