小编Arm*_*man的帖子

TypeScript 中的条件类型

我想知道我是否可以在 TypeScript 中使用条件类型?

目前我有以下界面:

interface ValidationResult {
  isValid: boolean;
  errorText?: string;
}
Run Code Online (Sandbox Code Playgroud)

但我想删除errorText,而当只有它isValidfalse一个必需的属性。

我希望我能够将其编写为以下界面:

interface ValidationResult {
  isValid: true;
}

interface ValidationResult {
  isValid: false;
  errorText: string;
}
Run Code Online (Sandbox Code Playgroud)

但如你所知,这是不可能的。那么,您对这种情况有何看法?

javascript types typescript

72
推荐指数
3
解决办法
6421
查看次数

标签 统计

javascript ×1

types ×1

typescript ×1