“'public'只能在.ts文件中使用”in typescript tslint react setup?

问题描述 投票:0回答:2

我无法解决为什么在VSCode中使用react typescript项目,使用tslint设置,我收到错误:

'public'只能在.ts文件中使用。

[实际上也是为什么我没有得到通常的变量a从来没有读过/使用警告/错误]

enter image description here

tsconfig.json

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-tslint-plugin"
      }
    ]
  },
}

tslint.json

{
    "defaultSeverity": "error",
    "extends": ["tslint:recommended", "tslint-react"],
    "jsRules": {},
    "rules": {
        "semicolon": [true, "never"]
    },
    "rulesDirectory": []
}

的package.json

{
  "name": "test-amplify-1",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.1",
    "@types/node": "11.9.0",
    "@types/react": "16.8.2",
    "@types/react-dom": "16.8.0",
    "react": "^16.8.1",
    "react-dom": "^16.8.1",
    "react-scripts": "2.1.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "tslint": "^5.12.1",
    "tslint-react": "^3.6.0",
    "typescript": "^3.3.3",
    "typescript-tslint-plugin": "^0.3.1"
  }
}

vscode扩展

enter image description here

内置的(即没有触及)

enter image description here

已经设置了本地包(typescript / tslint),但是参考re globals:

enter image description here

typescript visual-studio-code tslint
2个回答
0
投票

默认情况下没有启用tsx我想,您是否尝试将jsx添加到tsconfig文件中?

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    // "target": "es5",
    "removeComments": true,
    "jsx": "react",
    "allowJs": false,
    "baseUrl": "src",
    "experimentalDecorators": true,
    "alwaysStrict": true,
    "lib": [
      "es2015"
    ],
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules"
  ],
  "awesomeTypescriptLoaderOptions": {
    "useCache": true,
    "useTranspileModule": true,
    "transpileOnly": true
  }
}

0
投票

您的问题可能已在此处得到解答。以下是我认为的更多细节。 js 'types' can only be used in a .ts file - Visual Studio Code using @ts-check 我可以在最后看到我正在运行tslint 1.0.0版本。此外,您的打字稿版本可能有问题。如果您使用的是typescript 2.3或更早版本,则在启用潮汐时,html文件将被视为打字稿文件。将typescript更新到2.4.1。还要更新typescript / language-service。将以下内容添加到tsconfig.json。这可能是因为你的角度版本。 你关于为什么a变量抱怨为unused的问题,答案是你已经声明了一个变量但在代码中没有使用它,你可以忽略这样的警告。只要在代码中的某处使用该变量,该警告就会消失。因此,更改角度版本,看看是否会发出警告。通常tslint应该检测.ts文件和工作。

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