【问题标题】:Error: Cannot find module when using decorators错误:使用装饰器时找不到模块
【发布时间】:2021-07-02 06:58:34
【问题描述】:

我正在尝试在我的打字稿类中使用装饰器。 这就是我正在尝试的

import {
  Model,
  ModelCtor
} from 'sequelize/types';

function decorator(constructor) {
  // 
}

@decorator
class Service implements IService {
  // when using 'private User:  any' instead it works fine 
  constructor(private User: ModelCtor < Model < any, any >> ) {
      
  }
}

我收到以下错误:Error: Cannot find module 'sequelize/types' 在这个特定的示例中,我正在导入 sequelize/types,但它对任何其他 d.ts 文件的工作方式都相同。

我已经安装了类型并且在没有装饰器的情况下工作正常。此外,在为构造函数参数定义类型为any 时也不会抱怨:private User: any

我搜索了但没有找到有用的东西。

顺便说一下,我的 tsconfig.json 是:

{
"compilerOptions": {
 "target": "es5",
 "module": "commonjs",
 "outDir": "./build",
 "sourceMap": true,
 "typeRoots": [
   "./node_modules/@types",
   "./lib/types/index",
   "./node_modules/sequelize/types/"
 ],
 "allowSyntheticDefaultImports": true,
 "experimentalDecorators": true,
 "emitDecoratorMetadata": true,
 "forceConsistentCasingInFileNames": true,
 "moduleResolution": "node",
 "pretty": true,
 "allowJs": true,
 "noEmit": false,
 "esModuleInterop": true,
},
"include": [
 "./index.ts",
 "./lib"
],
"exclude": [
 "node_modules",
 "tests",
 ".vscode",
 "doc",

]
}

【问题讨论】:

  • 您好,您找到解决方案了吗?

标签: typescript typescript-decorator


【解决方案1】:

我正在编译带有 --transpile-only 标志的 TypeScript。

通过删除此标志解决了我的具体情况

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    相关资源
    最近更新 更多