首页 > 解决方案 > 始终指定变量类型

问题描述

我正在使用 TypeScript、ESLint 和 @typescript-eslint。我想始终指定变量类型并在我不指定变量类型时出现错误:

const myVariable = 1; // -> error
const myVaribale: number = 1 // -> ok

我在互联网上找不到任何东西。

标签: typescripteslinttypescript-eslint

解决方案


为我工作

添加到 .eslint.js

'@typescript-eslint/typedef': [
      'error',
      {
        arrayDestructuring: true,
        arrowCallSignature: true,
        arrowParameter: true,
        callSignature: true,
        memberVariableDeclaration: true,
        parameter: true,
        propertyDeclaration: true,
        objectDestructuring: true,
        variableDeclaration: true,
        variableDeclarationIgnoreFunction: true,
      },
    ],
'@typescript-eslint/no-inferrable-types': 0,

推荐阅读