Warm tip: This article is reproduced from serverfault.com, please click

javascript-类型“模型”上不存在属性“ tableName”

(javascript - Property 'tableName' does not exist on type 'Model')

发布于 2020-11-28 10:41:27

我正在尝试将javascript代码转换为打字稿。我有一个曾经在js中正常工作的类

class Model {

    constructor(input, alias) {
        this.tableName = input;
        this.alias = alias;
    }
}

但是现在我得到两个错误,说Property 'tableName' does not exist on type 'Model'Property 'alias' does not exist on type 'Model'

这也是我的tsconfig设置:

{
    "compilerOptions": {
      "module": "CommonJS",
      "target": "ES6",
      "allowSyntheticDefaultImports": true,
      "esModuleInterop": true,
      "moduleResolution": "Node",
      "noImplicitAny": false,
      "strict": false,
      "outDir": "./dist"
    },
    "include": ["controllers", "models", "module","routes","./app.ts"],
    "ts-node": {
      "transpileOnly": true
    }
  }

我对打字稿有点陌生,对此了解不多。

任何帮助将不胜感激

Questioner
amiryeg1
Viewed
11
farvilain 2020-11-28 18:43:48

你必须定义属性

class Model {
    tableName:string;
    alias:string;
    constructor(input, alias) {
        this.tableName = input;
        this.alias = alias;
    }
}

https://www.typescriptlang.org/docs/handbook/classes.html