Let’s combine some types and test for them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
type Item = { item: string, index: number } type Another = Item & { isVisible: boolean } const test: Another = { item: "heh", index: 3, isVisible: true } type Receipt = { total: number, id: string } const isItem = (info: Receipt | Item): boolean => { let res = (info as Item).item !== undefined; return res; } const a: Item = { item: "driver", index: 3 } const b: Another = { item: "screw", index: 8, isVisible: false } const c: Receipt = { total: 2434, id: "FJSK2SJ242" } console.log(isItem(a)); // true console.log(isItem(b)); // true console.log(isItem(c)); // false |