【发布时间】:2017-05-30 11:08:04
【问题描述】:
我想知道foo和bar的区别在以下界面:
interface Test
{
foo( value: number): string;
bar: ( value: number ) => string;
}
let x: Test = {
foo: ( i ) => "",
bar: ( i ) => "",
};
很明显,第一个是方法,而第二个是属性,但这在语义上是否相同?
编辑:
它似乎并不完全等价。至少对于构造函数方法now,只有第一个语法似乎是有效的:
class Test
{
}
interface TestConstructor
{
new(): Test;
}
const activator = function( type: TestConstructor )
{
return new type(); // fine
}
interface TestConstructor2
{
new: () => Test;
}
const activator2 = function( type: TestConstructor2 )
{
return new type(); // Error: Cannot use 'new' with an expression whose type lacks a call or construct signature.
}
【问题讨论】:
标签: typescript interface