首页 > 解决方案 > MyClass 类型上不存在属性 myProp:Typescript

问题描述

我在打字稿中有简单的课程

export class UserController {

    
    constructor(private service: UserService) {        
        
    }

    async insert(req, res: Response) {
       
    }
}

并且该类在一个文件 index.ts 中这样声明

import { UserService } from "./UserService";
import { UserController } from "./UserController";


const service = new UserService()
const userController = new UserController(service)

export {userController}

这就是控制台上的错误:

使用 ts-node 版本 8.10.2,typescript 版本 3.9.7 [错误] 10:37:49 ⨯ 无法编译 TypeScript:src/useCases/User/UserController.ts:6:14 - 错误 TS2339:属性“服务”确实'UserController' 类型上不存在。

     this.service = service;

如果很重要,那就是我的 tsconfig.json 和 package.json

{
  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "lib": [
      "es6"
    ],
    "allowJs": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "removeComments": true,
    "strict": false,
    "typeRoots": [
      "./node_modules/@types",
      "./src/@types"
    ],
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "@controllers/*": [
        "./src/controllers/*"
      ],
      "@entities/*": [
        "src/entities/*"
      ],
      "@config/*": ["src/config/*"],
      "@repositories/*": ["src/repositories/*"],
      "@useCases/*": ["src/useCases/*"],
      "@utils/*": ["src/utils/*"]
    }
  },
  "include": [
    "./src/**/*"
  ]
}

包.json

{
  "name": "area-do-cliente",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "dev": "ts-node-dev -r tsconfig-paths/register --respawn --transpile-only --ignore-watch node_modules --no-notify src/server.ts",
    "start": "node dist/server.js",
    "test": "jest",
    "build": "babel src --extensions \".js,.ts\" --out-dir dist --copy-files --no-copy-ignored"
  },
  "devDependencies": {
    "@babel/cli": "^7.10.5",
    "@babel/core": "^7.11.0",
    "@babel/node": "^7.10.5",
    "@babel/preset-env": "^7.11.0",
    "@babel/preset-typescript": "^7.10.4",
    "@types/express": "^4.17.7",
    "@types/jest": "^26.0.8",
    "babel-plugin-module-resolver": "^4.0.0",
    "eslint": "^7.6.0",
    "jest": "^26.2.2",
    "ts-jest": "^26.1.4",
    "ts-node-dev": "^1.0.0-pre.56",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.9.7"
  },
  "dependencies": {
    "@types/bcrypt": "^3.0.0",
    "@types/dotenv": "^8.2.0",
    "@types/jwt-simple": "^0.5.33",
    "@types/knex": "^0.16.1",
    "@types/nodemailer": "^6.4.0",
    "@types/passport": "^1.0.4",
    "@types/passport-jwt": "^3.0.3",
    "@typescript-eslint/eslint-plugin": "^3.8.0",
    "@typescript-eslint/parser": "^3.8.0",
    "auth": "../modules/auth/dist/auth",
    "bcrypt": "^5.0.0",
    "eslint-config-standard": "^14.1.1",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.1",
    "express": "^4.17.1",
    "jwt-simple": "^0.5.6",
    "knex": "^0.21.2",
    "nodemailer": "^6.4.11",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "pg": "^8.3.0",
    "uuidv4": "^6.2.1"
  }
}

我没有更多的想法要做。有人帮忙吗?

标签: javascriptnode.jstypescriptapitypes

解决方案


推荐阅读