typescript 序列化设置关联所有者ID“编号|{ [创建属性分支:true; }”不能赋给数字

8mmmxcuj  于 2022-12-24  发布在  TypeScript
关注(0)|答案(2)|浏览(32)

复制TypeScript手册时,在手动设置ownerId时使用Project.create(),它会出错。

const ab = Project.create({ name: 'abc', ownerId: newUser.get('id') })

错误Type 'number | { [CreationAttributeBrand]: true; }' is not assignable to type 'number'. Type '{ [CreationAttributeBrand]: true; }' is not assignable to type 'number'.使用内置的user.createProject可以正常工作,但手动尝试创建一个独立的不能正常工作。我理解为什么会发生这种情况,但我需要一个解决方案,因为我拥有的实际模型有多个关联要创建。
您可以通过复制https://sequelize.org/master/manual/typescript.html示例并添加上面的代码来获得相同的代码。

btqmn9zl

btqmn9zl1#

这是Sequelize v6.13中的一个错误。它后来被修复了。更新版本修复了它。(返回值是number | { [CreationAttributeBrand]...},而它应该是number &

vmpqdwk3

vmpqdwk32#

对于我的情况,我将id设置为any:

export interface ProjectInfoDto {
  // id: number;
  id: any;
  name: string;
  ownerId: number;
}

相关问题