egg ts 中 model 的字段类型都是{}

Created on 17 Apr 2019  ·  3Comments  ·  Source: eggjs/egg

What happens?

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

Mini Showcase Repository(REQUIRED)

仓库地址: [email protected]:zhaofinger/test-egg-ts-model.git

model 定义

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;
}

service 中使用

import { Service } from 'egg';

/**
 * Test Service
 */
export default class User extends Service {
  public async list() {
    const { ctx } = this;
    ctx.model.User.create({ id: 1 });
  }
}

How To Reproduce

Steps to reproduce the behavior:

  1. yarn
  2. yarn dev
  3. 在service/user.ts中使用model操作

Expected behavior

  1. 使用的时候可以给出正确的类型提示校验

Context

  • Node Version: v10.15.0
  • Egg Version: 2.21.1
  • Plugin Name: egg-sequelize
  • Plugin Version: 4.3.1
  • Platform: macOS Mojave

Most helpful comment

这个跟 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;
};

All 3 comments

类型提示如下:
image

这个跟 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!

Was this page helpful?
0 / 5 - 0 ratings