|
@@ -14,15 +14,15 @@ function extend<First, Second>(first: First, second: Second): First & Second {
|
|
|
const result: Partial<First & Second> = {};
|
|
|
for (const prop in first) {
|
|
|
if (first.hasOwnProperty(prop)) {
|
|
|
- (<First>result)[prop] = first[prop];
|
|
|
+ (result as First)[prop] = first[prop];
|
|
|
}
|
|
|
}
|
|
|
for (const prop in second) {
|
|
|
if (second.hasOwnProperty(prop)) {
|
|
|
- (<Second>result)[prop] = second[prop];
|
|
|
+ (result as Second)[prop] = second[prop];
|
|
|
}
|
|
|
}
|
|
|
- return <First & Second>result;
|
|
|
+ return result as First & Second;
|
|
|
}
|
|
|
|
|
|
class Person {
|
|
@@ -151,11 +151,10 @@ else if (pet.fly) {
|
|
|
```ts
|
|
|
let pet = getSmallPet();
|
|
|
|
|
|
-if ((<Fish>pet).swim) {
|
|
|
- (<Fish>pet).swim();
|
|
|
-}
|
|
|
-else {
|
|
|
- (<Bird>pet).fly();
|
|
|
+if ((pet as Fish).swim) {
|
|
|
+ (pet as Fish).swim();
|
|
|
+} else if ((pet as Bird).fly) {
|
|
|
+ (pet as Bird).fly();
|
|
|
}
|
|
|
```
|
|
|
|
|
@@ -170,7 +169,7 @@ TypeScript里的*类型守卫*机制让它成为了现实。
|
|
|
|
|
|
```ts
|
|
|
function isFish(pet: Fish | Bird): pet is Fish {
|
|
|
- return (<Fish>pet).swim !== undefined;
|
|
|
+ return (pet as Fish).swim !== undefined;
|
|
|
}
|
|
|
```
|
|
|
|