繁体   English   中英

如何在 Typescript 中定义通用类型保护?

[英]How to define a generic Type Guard in Typescript?

我想为我的类定义一个接口,其中包含一个用作类型保护的isValidConfig函数。 但我不确定如何声明它。

我已经这样做了:

type AnyConfig = ConfigA | ConfigB | ConfigC;

public abstract isValidConfig<T extends AnyConfig>(config: AnyConfig): config is T;

  public abstract isValidConfig<T = AnyConfig>(config: T): config is T;

但我总是在实现中遇到错误,例如:

public isValidConfig<T extends ConfigA >(config: T): config is T {
    return config.type === TrainingTypes.A;
} /// Types of parameters 'config' and 'config' are incompatible.
      Type 'T' is not assignable to type 'ConfigA '.

是否有可能做到这一点? 我还没找到路

错误是因为你不能有一个对泛型强制执行的保护。 以下来自官方 TS 文档: https : //www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards

您可以执行以下操作来防范单个类型:


enum ConfigTypes {
  a = 'a',
  b = 'b',
  c = 'c'
}

interface ConfigA {
  field: number;
  type: ConfigTypes.a;
}

interface ConfigB {
  otherField: string;
  type: ConfigTypes.b;
}

interface ConfigC {
  yetAnotherField: string[];
  type: ConfigTypes.c;
}

type AnyConfig = ConfigA | ConfigB | ConfigC;

export function isValidConfigA(config: AnyConfig): config is ConfigA {
  return config.type === ConfigTypes.a;
}


值得补充的是,必须在编译时强制执行类型,因为 TypeScript 根本无法执行运行时检查(那时它已经被转换为 JavaScript,它执行动态(运行时)检查)。 换句话说,您只能防范特定的已知类型。

如果您想检查给定的预期配置是否为配置,则从上面的示例继续,您可以执行以下操作:

export function isValidConfig(config: AnyConfig): config is AnyConfig {
  return (
    config.type === ConfigTypes.a ||
    config.type === ConfigTypes.b ||
    config.type === ConfigTypes.c
  );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM