定义的model在使用的时候类型都是{}
例如:

仓库地址: [email protected]:zhaofinger/test-egg-ts-model.git
import { Application } from 'egg';
export default(app: Application) => {
const { INTEGER, STRING, DATEONLY, ENUM } = app.Sequelize;
const User = app.model.define('user', {
level: { type: INTEGER, defaultValue: 0 },
avatar: { type: STRING(1000), allowNull: false },
username: { type: STRING(50), allowNull: false },
gender: { type: ENUM('male', 'female', 'unknown'), defaultValue: 'unknown', allowNull: false },
birthday: DATEONLY,
password: STRING(100),
email: { type: STRING(50), unique: true },
phone: { type: STRING(50), unique: true },
}, {
freezeTableName: true,
paranoid: true,
});
return User;
}
import { Service } from 'egg';
/**
* Test Service
*/
export default class User extends Service {
public async list() {
const { ctx } = this;
ctx.model.User.create({ id: 1 });
}
}
Steps to reproduce the behavior:
Expected behavior
类型提示如下:

这个跟 egg 没关系,这个是 Sequelize 的声明导致的,要你自己指定 CreationAttributes 的类型,ts 是静态类型检查,是没法分析的出 id: { type: INTEGER, defaultValue: 0 } 是什么类型的
import { Application } from 'egg';
interface CreationAttributes {
id?: number;
level?: number;
avatar?: number;
username?: string;
gender?: string;
birthday?: string;
password?: string;
email?: string;
phone?: string;
}
export default(app: Application) => {
const { INTEGER, STRING, DATEONLY, ENUM } = app.Sequelize;
const User = app.model.define<{}, CreationAttributes, CreationAttributes>('user', {
level: { type: INTEGER, defaultValue: 0 },
avatar: { type: STRING(1000), allowNull: false },
username: { type: STRING(50), allowNull: false },
gender: { type: ENUM('male', 'female', 'unknown'), defaultValue: 'unknown', allowNull: false },
birthday: DATEONLY,
password: STRING(100),
email: { type: STRING(50), unique: true },
phone: { type: STRING(50), unique: true },
}, {
freezeTableName: true,
paranoid: true,
});
return User;
};
OK, tks!
Most helpful comment
这个跟 egg 没关系,这个是 Sequelize 的声明导致的,要你自己指定 CreationAttributes 的类型,ts 是静态类型检查,是没法分析的出
id: { type: INTEGER, defaultValue: 0 }是什么类型的