Egg: ts下无法使用egg-sequelize, 启动报错

Created on 24 Jun 2019  ·  9Comments  ·  Source: eggjs/egg

报错信息如下:
/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) & typeof Model'.
Type 'typeof Model' is not assignable to type 'new () => Model'.
Cannot assign an abstract constructor type to a non-abstract constructor type.

Most helpful comment

你确定你看了 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 ,不允许直接用,只能继承来用。

All 9 comments

给个能稳定复现的 repo

使用的是最新版本的依赖库:
"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) & typeof Model'.
Type 'typeof Model' is not assignable to type 'new () => Model'.
Cannot assign an abstract constructor type to a non-abstract constructor type.

给个能稳定复现的 repo

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

thanks a lot

方便的话,加我微信 waxyysys28

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

我参考了,也保错

你确定你看了 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 ,不允许直接用,只能继承来用。

ok, 原来如此,thanks

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

Was this page helpful?
0 / 5 - 0 ratings