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

类型断言这一页“类型断言 vs 类型声明“里“但是 Cat 并不兼容 Animal”是否错误? #212

Open
Web-Kevin opened this issue Jul 25, 2021 · 2 comments

Comments

@Web-Kevin
Copy link
Contributor

image
该页”类型断言的限制“里相同的代码已经说了两者相互兼容,为何下面就不兼容了?

@liweiook
Copy link

liweiook commented Jan 6, 2022

animal as cat ,cat as animal不代表两者互相兼容。只是animal兼容cat,所以animal可以断言为cat,cat可以断言为animal。而声明只能是cat声明为animal。
即动物不一定是猫,猫一定是动物。猫一定是动物这句话将猫转为动物,就是let tom: animal= cat;
而断言则多了一种情况,即我通过它的一系列行为(代码逻辑)断言这个动物一定是猫。

@liweiook
Copy link

liweiook commented Jan 6, 2022

只有一样的东西才互相兼容

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

2 participants