【发布时间】:2016-07-31 18:00:12
【问题描述】:
我正在使用 Codepen.io 对 Typescript 进行第一次测试,但我不明白为什么在使用接口声明函数签名和返回类型之后,我可以返回不同的类型而不会出现任何错误。
我错过了什么吗?
示例代码:
interface IPerson {
getFullName: () => void;
}
class Person implements IPerson{
constructor(
public name: string,
public surname: string
){}
getFullName() {
return this.name + ' ' + this.surname;
}
}
let p = new Person('John','Doe');
console.log(p.getFullName());
结果:
John Doe
问题: 即使我声明了 void 返回类型,为什么还要注销 'John Doe' (a string)?
【问题讨论】:
标签: typescript