Update assertion syntax in code examples pr967
This commit is contained in:
parent
a933e369cd
commit
2dd291d42d
@ -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;
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -453,7 +453,7 @@ interface Square extends Shape {
|
||||
sideLength: number;
|
||||
}
|
||||
|
||||
let square = <Square>{};
|
||||
let square = {} as Square;
|
||||
square.color = "blue";
|
||||
square.sideLength = 10;
|
||||
```
|
||||
@ -473,7 +473,7 @@ interface Square extends Shape, PenStroke {
|
||||
sideLength: number;
|
||||
}
|
||||
|
||||
let square = <Square>{};
|
||||
let square = {} as Square;
|
||||
square.color = "blue";
|
||||
square.sideLength = 10;
|
||||
square.penWidth = 5.0;
|
||||
@ -494,7 +494,7 @@ interface Counter {
|
||||
}
|
||||
|
||||
function getCounter(): Counter {
|
||||
let counter = <Counter>function (start: number): string { return '' };
|
||||
let counter = (function (start: number) { }) as Counter;
|
||||
counter.interval = 123;
|
||||
counter.reset = function () { };
|
||||
return counter;
|
||||
|
@ -128,8 +128,8 @@ function listenEvent(eventType: EventType, handler: (n: Event) => void) {
|
||||
listenEvent(EventType.Mouse, (e: MouseEvent) => console.log(e.x + ',' + e.y));
|
||||
|
||||
// Undesirable alternatives in presence of soundness
|
||||
listenEvent(EventType.Mouse, (e: Event) => console.log((<MouseEvent>e).x + ',' + (<MouseEvent>e).y));
|
||||
listenEvent(EventType.Mouse, <(e: Event) => void>((e: MouseEvent) => console.log(e.x + ',' + e.y)));
|
||||
listenEvent(EventType.Mouse, (e: Event) => console.log((e as MouseEvent).x + "," + (e as MouseEvent).y));
|
||||
listenEvent(EventType.Mouse, ((e: MouseEvent) => console.log(e.x + "," + e.y)) as (e: Event) => void);
|
||||
|
||||
// Still disallowed (clear error). Type safety enforced for wholly incompatible types
|
||||
listenEvent(EventType.Mouse, (e: number) => console.log(e));
|
||||
|
Loading…
Reference in New Issue
Block a user