简体   繁体   English

是否可以在 TypeScript 中的类型定义中进行类型检查

[英]Is it possible to have a type check in type definition in the TypeScript

I have a class for returning reactive forms as forms with available controls listed in IntelliSense.我有一个 class 用于返回反应性 forms 作为 forms 与 IntelliSense 中列出的可用控件。 That works fine for FormControls but what I would like to achieve is to have FormGroups that are part of queried parent FormGroup to be returned as TypedFormGroup<T[key]>, hope that makes sense, is there any way I can have some type of assertion like controls:: { [key in keyof T]: (AbstractControl & T[key] is string) | TypedFormGroup<T[key]>}这对 FormControls 很好,但我想要实现的是让作为查询的父 FormGroup 一部分的 FormGroups 作为 TypedFormGroup<T[key]> 返回,希望这是有道理的,有什么方法可以让我拥有某种类型的类似controls:: { [key in keyof T]: (AbstractControl & T[key] is string) | TypedFormGroup<T[key]>} controls:: { [key in keyof T]: (AbstractControl & T[key] is string) | TypedFormGroup<T[key]>} ? controls:: { [key in keyof T]: (AbstractControl & T[key] is string) | TypedFormGroup<T[key]>}

export class TypedFormGroup<T> extends FormGroup {
  controls!: { [key in keyof T]: AbstractControl }

  constructor(controls: { [key in keyof T]: AbstractControl }) {
    super(controls);
  }
}

Found solution to my problem找到了我的问题的解决方案

export interface TypedFormGroup<T> extends FormGroup {
  controls: { [key in keyof T]: T[key] extends (string | number | boolean) ? AbstractControl : TypedFormGroup<T[key]> }
}

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

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