Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync "Advanced Types.md" #156

Closed
zhongsp opened this issue Jan 6, 2017 · 4 comments
Closed

Sync "Advanced Types.md" #156

zhongsp opened this issue Jan 6, 2017 · 4 comments

Comments

@zhongsp
Copy link
Owner

zhongsp commented Jan 6, 2017

Done.

@xcatliu
Copy link

xcatliu commented Jan 16, 2017

type assertion
type predicate

这两个都翻译成类型断言可能会引起误解

@zhongsp
Copy link
Owner Author

zhongsp commented Jan 17, 2017

@xcatliu 感谢 我稍后review一下

@mishengqiang
Copy link
Contributor

@zhongsp type predicate是类型谓词,type assertion是类型断言。

@xcatliu
Copy link

xcatliu commented Jan 17, 2017

贴一段上下文:

To get the same code working, we’ll need to use a type assertion:

let pet = getSmallPet();

if ((<Fish>pet).swim) {
    (<Fish>pet).swim();
}
else {
    (<Bird>pet).fly();
}

To define a type guard, we simply need to define a function whose return type is a type predicate:

function isFish(pet: Fish | Bird): pet is Fish {
    return (<Fish>pet).swim !== undefined;
}

pet is Fish is our type predicate in this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants