我有一个类似如下的功能:在打字稿
var myFunction = function (config) {
var example = this.property; // just illustrating that we use `this`
}
myFunction.__reference = 'foobar';
现在我想要把它写在严格打字稿:
interface ExternalScope {
property: string;
}
interface ConfigObject {
name: string,
count: number
}
interface MyFunction {
(XHRLoader: this, cfg: ConfigObject): any;
__reference: string;
}
var myFunction = function (this: ExternalScope, config: ConfigObject): any {
var example = this.property;
}
myFunction.__reference = 'foobar';
使用上面的代码,我得到以下TypeScipt错误:
Property '__reference' does not exist on type '(this: ExternalScope: config: ConfigObject) => any
我tsconfig.json
相关部分:
"compilerOptions": {
"noEmitOnError": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"strictNullChecks": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"outDir": "./build",
"allowJs": false,
"target": "es5"
},
你还没有告诉TS'myFunction'是'MyFunction'的一个实例。 – deceze
我该怎么做?我试过:之前的功能。但是我认为我有什么不对,因为这给了我更多的错误。 –
'var myfunction:MyFunction = ...'会是最简单的。 – deceze