Typescript 如何省略错误:[ts]的推断类型<;类函数名>;引用不可访问的';这';类型。类型注释是必需的
我想创建非常漂亮、流畅的typescript api,但我正在 错误: [ts]推断的“firstLevelFunction”类型引用了不可访问的“this” 类型。类型注释是必需的 我的类架构:Typescript 如何省略错误:[ts]的推断类型<;类函数名>;引用不可访问的';这';类型。类型注释是必需的,typescript,visual-studio-code,typescript2.0,vscode-debugger,Typescript,Visual Studio Code,Typescript2.0,Vscode Debugger,我想创建非常漂亮、流畅的typescript api,但我正在 错误: [ts]推断的“firstLevelFunction”类型引用了不可访问的“this” 类型。类型注释是必需的 我的类架构: class SomeClassAPI { // in my case (on the picture at bottom) // 'firstLevelFunction(...)' equal 'replace(...)' method , // from class Co
class SomeClassAPI {
// in my case (on the picture at bottom)
// 'firstLevelFunction(...)' equal 'replace(...)' method ,
// from class CodeTransform )
public firstLevelFunction() {
const self = this; // ERROR
// const self = this as any; // OK but not typechecking
return {
secondLevelFunction() {
return {
thirdLevelFunction() {
// ....
}
}
}
}
}
}
只有当我使用“thirdLevelFunction”进行api时,我才能得到这个东西。
如何忽略此错误
我的tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"importHelpers": true,
"module": "commonjs",
"skipLibCheck": true,
"sourceMap": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"dom",
"es2015"
],
"types": [
"node"
],
"rootDir": "./src",
"outDir": "dist"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"preview",
"docs",
"dist",
"bundle",
"example",
"examples",
"tests",
"tmp-src"
]
}
这是vscode中的错误外观:
我正在使用:
- typescript2.6.2
- vscode1.21.1
- mac osx10.13.3
public replace:any(){…}
我猜TS在这种特定情况下无法推断返回类型。您能否更清楚地了解错误发生的确切位置?您在描述中没有提到同构的
或replace()
。此外,上面提供的代码不是有效的TypeScript(类定义周围没有括号)。也许发布一个可以重现问题的最小示例会有帮助。@err1100尝试在vscode中使用now classSomeClassAPI
和tsconig.json(如上)。复制和粘贴您提供的代码时,我没有收到任何来自vscode的intellisense错误,也没有收到来自tsc
的编译器错误,创建一个新的SomeClassAPI实例,并调用firstLevelFunction()
。嗯,我也遇到了这个错误。运气好吗?