首页 > 解决方案 > 打字稿没有抱怨关于没有在构造函数中声明类型

问题描述

我是打字稿的新手,我期待打字稿会因为没有为我的构造函数声明类型而引发错误,但不幸的是我不是......

所以我很好奇为什么我没有收到错误。

这就是我正在做的

export interface BaseConfig {
    app: express.Application, 
    routePermission: number,
    context: any
}

export class BaseConfig implements BaseConfig {
    constructor(
        context,
        authentication = false,
        authenticatedRoute = USER_TYPE.LOGGED_IN_NORMAL_USER
    ) {
        //intitalize Express App
        this.routePermission = authenticatedRoute

        this.context = context
        this.app = express()

这是我的tsconfig

{
  "compilerOptions": {
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "noImplicitAny": false
  },
  "compileOnSave": true,
  "include": [
    "src"
  ],
  "exclude": ["node_modules"]
}

标签: typescript

解决方案


noImplicitAny应该设置为真。当为 false 时,意味着隐式 any 被接受,并且没有类型声明的变量因此隐式地具有 type any


推荐阅读