javascript - 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"
}
}
我没有更多的想法要做。有人帮忙吗?
解决方案
推荐阅读
- javascript - Prettier 不在 vscode 上格式化角模板
- python - 如何在 Python 中访问子字典
- ios - 如何在iOS中将电话应用程序发送到后台
- android - 日期/时间选择器颤振:确定/取消按钮不可见
- google-analytics - 新段不检测流量
- c++ - std::reference_wrapper 上的类型特征
- css - 合并两个 css 文件会提高网页性能吗?
- mysql - JPA - 使用条件 JsonManagedReference 进行查询
- reactjs - 如何阻止反应滞后于重新渲染?
- ios - 转换后访问属性时带有 where 子句的协议崩溃