简体   繁体   English

为什么Typescript类会自动实现接口?

[英]Why do Typescript classes implement interfaces automatically?

So I did the Typescript tutorial without any former JS experience. 所以我没有任何以前的JS经验就完成了Typescript教程。 My question is in given example code, why can you pass the Student object into the greeter() function which takes a Person as parameter? 我的问题是在给定的示例代码中,为什么可以将Student对象传递给以Person作为参数的greeter()函数? The class Student never implements said interface so I wonder if in Typescript classes automatically implement interfaces. Student类从不实现所述接口,所以我想知道在Typescript类中是否自动实现接口。 And if they do, what's the reasoning behind this? 如果他们这样做,这背后的原因是什么? It seems pretty useless if Car, Plane and Student all automatically implement Person. 如果Car,Plane和Student都自动实现Person,那似乎毫无用处。

class Student {
    fullName: string;
    constructor(public firstName, public middleInitial, public lastName) {
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
}

interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person : Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

var user = new Student("Jane", "M.", "User");

document.body.innerHTML = greeter(user);

This is called Structural Typing . 这称为结构类型 Essentially, relationships between types in TypeScript are never required to be explicitly declared ('named', as in nominal typing, like C#, Java and friends), they're done purely by analysing the structure of the types involved. 从本质上讲,TypeScript中类型之间的关系永远不需要显式声明('命名',如在名义类型中,如C#,Java和朋友),它们纯粹是通过分析所涉及类型的结构来完成的。

In TypeScript when you say that a class implements an interface, you're not actually changing the class at all, or the types of the subclasses involved, you're just asking the compiler to confirm that it does indeed already implement that interface. 在TypeScript中,如果你说一个类实现了一个接口,你实际上根本没有改变它,或者所涉及的子类的类型,你只是要求编译器确认它确实已经实现了该接口。

As for the reasoning, the key factor here (as with many decisions in TypeScript) is that this more closely matches what JavaScript does in practice (ie duck typing - if you pass something the right shape, it'll work), so makes compatibility with existing JavaScript code far easier. 至于推理,这里的关键因素(与TypeScript中的许多决策一样)是这与JavaScript在实践中更接近匹配(即鸭子打字 - 如果你传递正确的形状,它会工作),所以兼容性使用现有的JavaScript代码更容易。

Notably this does leave TypeScript with some limitations. 值得注意的是,这确实使TypeScript有一些限制。 For example, you can't use identical but incompatible types as marker interfaces to limit input, as in Java. 例如,您不能使用相同但不兼容的类型作为标记接口来限制输入,就像在Java中一样。 In Java Serializable and Cloneable cloneable are two empty interfaces which can be implemented to mark a type as serializable or cloneable, and methods can then accept only Serializable parameters to ensure they get only classes which are explicitly known to be safe to serialize. 在Java中, SerializableCloneable cloneable是两个空接口,可以实现将类型标记为可序列化或可克隆,然后方法只接受Serializable参数,以确保它们只获得明确知道序列化安全的类。 In TypeScript, you can't do that: an empty interface doesn't change the structure of an object, so doesn't make any difference to the type system at all. 在TypeScript中,您不能这样做:空接口不会更改对象的结构,因此根本不会对类型系统产生任何影响。

interface in typescript is a pure abstract concept in really javascript world, it doesn't has interface concept. 打字稿中的界面是一个纯粹的抽象概念,在真正的javascript世界中,它没有界面概念。 the interface you write in typescript won't be translated into javascript 你在typescript中编写的界面不会被翻译成javascript

So, for interface, it doesn't have to really implement it, just need to be in the same shape. 因此,对于接口,它不必真正实现它,只需要具有相同的形状。 official explanation can be find here https://www.typescriptlang.org/docs/handbook/type-compatibility.html 官方解释可以在这里找到https://www.typescriptlang.org/docs/handbook/type-compatibility.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM