English 中文(简体)
适当使用 类型<MT>和交叉类型
原标题:Properly use Typescript Set<T> with intersecting types

我不理解为何转播者抱怨。 基本类型申报:

export enum SvgElementType {
  path = "path",
  circle = "circle",
}

export type SvgElement = {
  id: number;
  type: SvgElementType;
};

export type SvgPathElement = SvgElement & {
  type: SvgElementType.path;
  d: string;
};

export type SvgCircleElement = SvgElement & {
  type: SvgElementType.circle;
  cx: number;
  cy: number;
  radius: number;
};

export type Serializer<E extends SvgElement = SvgElement> = (element: E) => string;

const pathSerializer: Serializer<SvgPathElement> = e => "";
const circleSerializer: Serializer<SvgCircleElement> = e => "";

const serializers: Set<Serializer> = new Set();
serializers.add(pathSerializer); // <<<--- transpile error

// full TS error
// Argument of type  Serializer<SvgPathElement>  is not assignable to parameter of type  Serializer<SvgElement> .
//  Type  SvgElement  is not assignable to type  SvgPathElement .
//    Property  d  is missing in type  SvgElement  but required in type  { type: SvgElementType.path; d: string; } .ts(2345)

我发现的唯一方式是修改<代码>Serializer with any 作为缺省类型:

export type Serializer<E extends SvgElement = any> = (element: E) => string;

这为我指明了,保护<代码>航天器/代码>的最低打字方法可能更好。 3. 稍后采用......

最佳回答

转播员抱怨说,你实际上说,你想要一项包含<条码>的“SvgElement/code>的功能,作为投入,但随后,你又试图赋予它一项带有<条码”的职能。 SvgPathElement 。

这样做可以奏效——它将允许你通过<编码>SvgElement来履行你想要的职能。 SvgPathElement

“功能参数为连续。

您的代码将要求对标有序号的物体进行操作式的类型检查,因此,您或许应当重新界定“密码”“Serializer的类型,而没有通用名称? e.g:

export type Serializer = (element: SvgElement) => string;
问题回答

编辑们正在抱怨,因为如果允许编辑,你的法典会在操作时间造成类型的错误。 例如,如果有人要做的话:

function serialize(e: SvgElement) {
  for (const s of serializers) {
    return s(element);
  }
}

that would compile, since every member of the set serializers is declared to accept any kind of SvgElement - but that is not actually true, because each Serializer only accepts a particular type of element, and would fail with type errors at runtime if called with a different type.

因此,你如何写出正确的字句?

在我回答之前,我想到如何使用<条码>射线器,因为我略感惊讶的是,<条码>射动器是<条码>。 如何在本<代码>Set中找到正确的<代码>Serializer? 在我看来,<代码> 《<<<>条/代码>是这方面数据结构真正较差的,<>Map或字标的更直截了当的选择。

因此,我这样说:

export type SvgElementBase<T extends string> = {
  id: number;
  type: T;
}

export type SvgPathElement = SvgElementBase<"path"> & {
  d: string;
};

export type SvgCircleElement = SvgElementBase<"circle"> & {
  cx: number;
  cy: number;
  radius: number;
};

export type SvgElement = SvgPathElement | SvgCircleElement;
export type SvgElementType = SvgElement["type"];

export type Serializer<E extends SvgElement> = (e: E) => string;
export type Serializers = {[T in SvgElementType]: Serializer<SvgElement & SvgElementBase<T>>}

const serializers: Serializers = {
  path: (e) => `path ${e.d}`,
  circle: (e) => `circle at (${e.cx},${e.cy}) with radius ${e.radius}`,
}

function lookupSerializer<E extends SvgElement>(type: E["type"]): Serializer<E> { // the things we do for type safety
  return serializers[type];
}

export function serialize(element: SvgElement) {
  return lookupSerializer(element.type)(element);
}

通过宣布“<代码>射线/代码”为地图型,我们可以表示,钥匙的类型和价值是相关的,即特定类型登记的序列器只接受这种特定类型的数值。 通用的<代码>l/代码>功能使汇编者能够实际分发该联盟,如果您在网上公布,则汇编者不会分发该联盟,因此尽管该职能得到微薄的执行,实际上是必要的。





相关问题
Generic constraint exclusion

Sorry for asking silly question Is it possible to enforce constraint on generic in such a way that the given T can be derived from any reference Type except some A,B,C (where A,B,C are reference ...

Can a Type Derive from Itself?

When enforcing a Generic constraint class GenericTest { public void Sample<T>(T someValue) where T:Racer,new() { Console.WriteLine(someValue.Car); } } The Type T should be ...

C# Deriving Generic Methods

When i need to pass a Generic Type I can use the Syntax (Example : Ofcourse it is not a Generic Method) public void Sample(T someValue) { ...... } What is the benefit of declaring Sample<T>...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

热门标签