【问题标题】:Property 'setComputed' is optional in type ... but required in type 'EntityExample'属性“setComputed”在类型中是可选的......但在类型“EntityExample”中是必需的
【发布时间】:2020-10-20 21:23:44
【问题描述】:

我正在尝试编写一个工厂来更新实体,但使用后加载会引发错误:

实体:

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  OneToMany,
  BaseEntity,
  AfterLoad,
} from "typeorm";
import { OtherEntity } from "./OtherEntity";

// ColumnNumericTransformer
export class ColumnNumericTransformer {
  to(data: number): number {
    return data;
  }
  from(data: string): number {
    return parseFloat(data);
  }
}

@Entity()
export class EntityExample extends BaseEntity {
  @PrimaryGeneratedColumn("uuid")
  id: string;

  @Column()
  name: string;

  @Column("numeric", {
    precision: 7,
    scale: 2,
    transformer: new ColumnNumericTransformer(),
    default: 0,
  })
  GGR: number;

  @Column("numeric", {
    precision: 7,
    scale: 2,
    transformer: new ColumnNumericTransformer(),
    default: 0,
  })
  bets: number;

  @Column("numeric", {
    precision: 7,
    scale: 2,
    transformer: new ColumnNumericTransformer(),
    default: 0,
  })
  wins: number;

  @Column({ default: 0 })
  fee_percentage: number;

  @Column("numeric", {
    precision: 7,
    scale: 2,
    transformer: new ColumnNumericTransformer(),
    default: 0,
  })
  fee_amount: number;

  @OneToMany((type) => OtherEntity, (some) => some.data)
  relations: OtherEntity[];

  @AfterLoad()
  setComputed() {
    this.GGR = this.bets - this.wins;
    this.fee_amount = this.GGR * (this.fee_percentage/100);
  }
}

工厂:

update: async(attrs: Partial<EntityExample> = {}) => {
    let entityExample = await getRepository(EntityExample).findOne(attrs.id);
    entityExample = {...entityExample, ...attrs};
    let updated_entity_example;

    try {
        updated_entity_example = await getRepository(EntityExample).save(entityExample);
    } catch (e) {
      throw new Error("Couln't save Entity Example")
    }
    return updated_entity_example
  }

TypeORM 错误:

Property 'setComputed' is optional in type '{ id: string; name: string; GGR: number; bets: number; wins: number; fee_percentage: number; fee_amount: number; setComputed?: () => void; hasId?: () => boolean; save?: (options?: SaveOptions) => Promise<...>; remove?: (options?: RemoveOptions) => Promise<...>; softRemove?: (options?: SaveOptions...' but required in type 'EntityExample'

那么,问题是我该如何解决呢?我应该每次都将 setComputed 传递给工厂吗?或者,还有更好的方法?也许值得使用不同的方法?哪些比较好?

【问题讨论】:

    标签: node.js typescript typeorm


    【解决方案1】:

    我认为这里的问题是部分意味着属性可以是未定义的,如果你这样做 {...{ foo: 'bar'}, ...{ foo: undefined}} 那么你会得到 { foo:不明确的 }。因为这个打字稿将使用可能未定义的类型选项。由于函数属性可能不会在 attrs 上传递,因此您可以省略具有函数签名的任何内容并使其正常工作,因此类似于

    update: async(attrs: Partial<OmitOnType<EntityExample, (...args: any) => any>> = {}) => {
    

    编辑:Typescripts Omit 不会接受任何不是字符串数字或符号的内容,为了能够省略类型,您可以使用如下所示的类型映射

     type OmitOnType<T, U> = {
         [P in keyof T]: T[P] extends U ? never : P
    };
    

    【讨论】:

    • 我在编辑Type '(...args: any) =&gt; any' does not satisfy the constraint 'string | number | symbol'.时收到此消息
    • 正确的原生 typescript Omit 不接受这样的类型,您可以创建自己的实用程序类型,我已将其添加到允许您执行此操作的注释中。
    • 这样显示类型错误:Types of property 'game_id' are incompatible. Type 'number | "game_id"' is not assignable to type 'number'.
    • await getRepository(...).findOne()的返回类型是什么;
    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2021-04-26
    • 2021-07-26
    • 1970-01-01
    • 2019-09-19
    • 2022-12-26
    • 2020-12-25
    相关资源
    最近更新 更多