接口和具体类型变量是可以互换的,虽然我在类和接口中定义了名称,但是编译器在抱怨.
界面Ibar
interface IBar { // IP
readonly name: string;
}
班级栏
class Bar implements IBar { // P
protected readonly _name: string;
public get name(): string {
return this._name;
}
}
Use
let bar = new Bar(); // concrete type
let factory: () => IBar = () => new Bar(); // interface type
// assigning an interface type result to concrete type var
bar = factory(); // TS2739: Type IBar is missing the following properties from type Bar: _name, name