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
| interface Waiter { anjiao: boolean say: () => {} }
interface Teacher { anjiao: boolean skill: () => {} }
function judgeWho(animal: (Waiter | Teacher)) { if (animal.anjiao) { (animal as Teacher).skill() } else { (animal as Waiter).say() }
if ('skill' in animal) { animal.skill() } else { animal.say() }
}
class NumberObj { count: number } function addObj(first: object | NumberObj, second: object | NumberObj) { if (first instanceof NumberObj && second instanceof NumberObj) { return first.count + second.count; } return 0; }
|