【发布时间】:2016-02-16 22:17:24
【问题描述】:
在打字稿中,以下编译。
class Person {
name : string;
}
class Employee {
name : string;
}
var person : Person = new Employee();
为什么会这样??这不是:
class Person {
name : string;
surname : string;
}
class Employee {
name : string;
}
var person : Person = new Employee();
除了:
类型employee不可分配给类型Person,属性姓氏 Employee 类型中缺少
所以。在编译时使用属性名称比较类型??!
编辑:是的。这是来自 typescript 编译器的代码进行检查。
for (var i = 0; i < targets.length; i++) {
var related = isRelatedTo(sources[i], targets[i], reportErrors);
if (!related) {
return 0;
}
result &= related;
}
return result;
【问题讨论】:
-
我正要写一个答案,但那里有不准确之处,因为我很少使用 TypeScript,所以我将发布一个链接到Type Compatibility
[...]Type compatibility in TypeScript is based on structural subtyping. Structural typing is a way of relating types based solely on their members. This is in contrast with nominal typing.[...]
标签: javascript typescript