定义接口(interface)方法之间有什么实际区别:
interface Foo {
bar(): void;
}
并定义一个函数类型的属性:
interface Foo {
bar: () => void;
}
?
最佳答案
还有一个区别,readonly
修饰符不能应用于方法。因此,无法阻止以下分配:
interface Foo {
bar(): void;
}
declare var x: Foo;
x.bar = function () { };
如果 bar
被定义为属性,则可以对其应用 readonly
修饰符:
interface Foo {
readonly bar: () => void;
}
防止重新分配。
( Playground )
关于typescript - 函数属性与方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39156315/