English 中文(简体)
工会类型
原标题:TypeScript use union of union
  • 时间:2024-02-26 23:26:19
  •  标签:
  • typescript

请允许我帮助我了解这里发生的情况吗?

type A = { propA: string };
type B = { propB: number }

type U = A | B;

class Model {}

class SingleModel extends Model {

  constructor(input: U) {
    super();
  }
}

class MultiModel extends Model {

  constructor(input: U[]) {
    super();
  }
}

type sheetType = new (input: U | U[]) => Model;

const config: { sheet: sheetType }[] = [
  { sheet: MultiModel },
  { sheet: SingleModel },
];

这里正出现错误:TS:2322 on sheetpropp in config 这里有什么错误?

最佳回答

单独书写两种构造的结合,而不是必须接受的单一类型。 U和U[]作为参数。

type sheetType = (new (input: U) => Model) | (new (input: U[]) => Model);
问题回答

您还可以提取两种可读性建筑类型:

type SingleSheetType = new (input: U) => Model;
type MultiSheetType = new (input: U[]) => Model;

type sheetType = SingleSheetType | MultiSheetType;

const config: { sheet: sheetType }[] = [
  { sheet: MultiModel },
  { sheet: SingleModel },
];




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签