gpt4 book ai didi

typescript - 如何省略错误 : [ts] The inferred type of references an inaccessible 'this' type. 需要类型注释

转载 作者:行者123 更新时间:2023-12-03 21:24:13 26 4
gpt4 key购买 nike

我想创建非常漂亮、流畅的 typescript api,但我得到了
错误:

[ts] The inferred type of 'firstLevelFunction' references an inaccessible 'this' type. A type annotation is necessary.



我的类(class)架构:
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 中的样子:

enter image description here

我在用:
  • typescript 2.6.2
  • vscode 1.21.1
  • mac osx 10.13.3
  • 最佳答案

    我刚刚收到此错误消息。
    当您明确指定函数返回值的类型时,它就会消失:public replace: any() {...}我猜 TS 无法推断出这种特定情况下的返回类型。

    关于typescript - 如何省略错误 : [ts] The inferred type of <class function name> references an inaccessible 'this' type. 需要类型注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49499691/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com