类型中的TypeScript可选泛型



我很好奇我能以某种方式使这个泛型可选吗?

export type RequestType<T> = {
readonly type: string;
readonly value: string | T;
};

因为当我尝试将对象的类型设置为RequestType时,它会返回一个错误,该泛型类型需要1个类型参数。

您可以为T提供默认类型。例如:

export type RequestType<T = boolean> = {
readonly type: string;
readonly value: string | T;
}

游乐场链接

如果您愿意,默认值甚至可以是string,因此value将是string | string,也称为string

最新更新