I use the "as" keyword and then a specific property on the type.
export function isWeapon(item: Item | Weapon | Armor | Shield): item is Weapon { return (item as Weapon).damageDice !== undefined; }
so in your above example you could leave out the "kind" property:
interface Cat { numberOfLives: number; } interface Dog { isAGoodBoy: boolean; } function isCat(animal: Cat | Dog): animal is Cat { return (animal as Cat).numberOfLive !== undefined; }
I like not having to add the extra property to my types. :)
Full code for my example found in this file: github.com/mirkoRainer/RulesLawyer...
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.
I use the "as" keyword and then a specific property on the type.
so in your above example you could leave out the "kind" property:
I like not having to add the extra property to my types. :)
Full code for my example found in this file: github.com/mirkoRainer/RulesLawyer...