functiongoToParty (place: PartyZone): string{ return`lets go to the ${place}`; }
goToParty("pizza hut"); goToParty("chuck e. cheese"); // Argument of type `"chuck e. cheese"' is not assignable to parameter of type 'PartyZone'
交叉类型
交叉类型是两种或更多种类型的组合。 适用于需要实现多个接口的对象和参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
interface Kicker { kick(speed: number): number; }
interface Puncher { punch(power: number): number; } // assign intersection type definition to alias KickPuncher type KickPuncher = Kicker & Puncher;
functionattack (warrior: KickPuncher) { warrior.kick(102); warrior.punch(412); warrior.judoChop(); // Property 'judoChop' does not exist on type 'KickPuncher' }