TypeScript版本: 2.0.3
码
interface UIElement {
addClickListener(onclick: (this: UIElement, e: Event) => void): void;
prop: number;
}
let uiElement: UIElement;
uiElement.addClickListener(() => {
var k = this.prop;
});
预期行为:
没错
实际行为:
tsc.exe --noImplicit此等等
blah.ts(8,10):错误TS2683:'this'隐式具有类型'any',因为它没有类型注释。
在您的示例中,您使用的是箭头功能-因此该功能内部可用的this
与外部this
-没有类型信息(任何)
使用function
,您将输入以下内容:
interface UIElement {
addClickListener(onclick: (this: UIElement, e: Event) => void): void;
prop: number;
}
let uiElement: UIElement;
uiElement.addClickListener(function() {
var k = this.prop;
});
这种行为对我来说看起来不错。 它确实捕获与滥用箭头功能有关的错误。
最有用的评论
在您的示例中,您使用的是箭头功能-因此该功能内部可用的
this
与外部this
-没有类型信息(任何)使用
function
,您将输入以下内容:这种行为对我来说看起来不错。 它确实捕获与滥用箭头功能有关的错误。