[eggjs/egg]ts下无法使用egg-sequelize, 启动报错

2025-11-04 531 views
6

报错信息如下: /Users/xian2/kpl/node/egg-office/node_modules/ts-node/src/index.ts:261 return new TSError(diagnosticText, diagnosticCodes) ^ TSError: [egg-core] load file: /Users/xian2/kpl/node/egg-office/app/service/user.ts, error: ⨯ Unable to compile TypeScript: app/service/user.ts(7,12): error TS2684: The 'this' context of type 'typeof Model' is not assignable to method's 'this' of type '(new () => Model<unknown, unknown>) & typeof Model'. Type 'typeof Model' is not assignable to type 'new () => Model<unknown, unknown>'. Cannot assign an abstract constructor type to a non-abstract constructor type.

回答

4

给个能稳定复现的 repo

3

使用的是最新版本的依赖库: "egg": "^2.22.2", "egg-scripts": "^2.11.0", "egg-sequelize": "^5.1.0", "egg-bin": "^4.13.0", 执行npm run tsc报错如下: app/service/user.ts:7:12 - error TS2684: The 'this' context of type 'typeof Model' is not assignable to method's 'this' of type '(new () => Model<unknown, unknown>) & typeof Model'. Type 'typeof Model' is not assignable to type 'new () => Model<unknown, unknown>'. Cannot assign an abstract constructor type to a non-abstract constructor type.

4

给个能稳定复现的 repo

https://github.com/yibiankeji/egg-office.git

thanks a lot

5

方便的话,加我微信 waxyysys28

7

eggjs/example 项目下有个 sequelize-ts 示例,参考那个

4

我参考了,也保错

1

你确定你看了 demo 么,model 的定义需要这样写

export default function(app: Application) {
  const { STRING, INTEGER, DATE } = app.Sequelize;
  const User = app.model.define('user', {
      ...
  });

  return class extends User {
     // ...
  };
}

define 返回的类型是 Model 这个类,而这个在 sequelize 的声明中是 abstract class ,不允许直接用,只能继承来用。

7

ok, 原来如此,thanks

5

@whxaxes 中文文档里面任然是直接 return User的,建议更新下文档吧,不然会误导很多人