VSCode中的Typescript服务器错误地报告未使用的私有变量

问题描述 投票:1回答:1

为什么typescript报告该变量未被使用,即使它在_close()方法中被引用?

奇怪的是,如果我删除private关键字,警告就会消失。

请注意,即使它是一个警告,它仍然无法编译。

enter image description here

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "noUnusedLocals": true, <-- This line
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}
typescript visual-studio-code
1个回答
4
投票

打字稿抱怨你从未读过变量。你的_close方法只设置它。而且我认为打字稿正确地抱怨,如果您以后从未对使用它的价值感兴趣,那么存储内容的重点是什么?

此外,如果删除private修饰符,则错误消失,因为那时具有该类实例的任何人都可以读取该值。因此,typescript不再能够验证该值是否永远不会被读取(因为它可能被其他人使用)。但是使用private,它只知道类方法可以访问它,所以如果它没有看到值在类本身的任何地方读取,它将产生错误。

© www.soinside.com 2019 - 2024. All rights reserved.