const model = { user_id: { type: INTEGER(11), primaryKey: true, autoIncrement: true, }, name: STRING(30), // 名字 createdAt: DATE, updatedAt: DATE, deletedAt: DATE, }
const User = app.model.define('user', model, { paranoid: true, freezeTableName: true, underscored: false, }) User.associate = function() { } User.model = model return User }
这时候 ReturnType<typeof ExportUser>的类型就是any,于是
![image](https://user-images.githubusercontent.com/26317926/61524130-5dd33200-aa48-11e9-9dc0-8d288564c1dd.png)
## 思路
> 目前有两种思路
1. 在每个model文件里添加注释
```js
'use strict'
/**
* @param {Egg.Application} app
*/
module.exports = app => {
const {
INTEGER,
DATE,
STRING,
} = app.Sequelize
const model = {
user_id: {
type: INTEGER(11),
primaryKey: true,
autoIncrement: true,
},
name: STRING(30), // 名字
createdAt: DATE,
updatedAt: DATE,
deletedAt: DATE,
}
const User = app.model.define('user', model, {
paranoid: true,
freezeTableName: true,
underscored: false,
})
User.associate = function() {
}
User.model = model
return User
}
- d.ts里增加一个可以自定义的CusModel
//egg-sequelize //index.d.ts declare module 'egg' { interface CusModel extends sequelize.ModelCtor<any> }
然后在egg-ts-helper生成的model添加
declare module 'egg' { interface CusModel {} interface IModel { User: (any extends ReturnType<typeof ExportUser> ? CusModel : ReturnType<typeof ExportUser>) & CusModel } }
第一种不需要修改插件和egg-ts-helper,第二种类型提示可以由插件控制,同时保留了model里面返回的类型
跟进- [ ] some task
- [ ] PR URL