typescript - 打字稿:如何从盒子的联合中提取类型?
问题描述
我有一些 Box 类型和 Union,其中包括装箱值和原始值。如何提取盒装类型?
class Box<T> {};
type United = Box<number> | Box<string> | boolean;
type Unboxed<T> = ???
type ExtractUnited = Unboxed<United>; // number | string
解决方案
首先,你必须T
在你的Box
类中使用:
class Box<T> {
constructor(public value: T) {}
};
二、声明Unboxed<T>
为:
type Unboxed<T> = T extends Box<infer U> ? U : never;
推荐阅读
- amazon-web-services - Kafka AWS 部署,如何在不停机的情况下更改磁盘大小?
- python - 如何在python中合并这个数据框?
- vba - Filtering another form based on fixed value
- azure - Azure 存档操作和定价
- html - Box-shadow 与 div top/down/left/bottom 周围的其他 div 重叠吗?
- python - 在 Python 中返回格式正确的递归列表
- twilio - 希望接听拨打 twilio 美国电话号码的电话(我在国外)
- neo4j - neo4j 为现有节点生成唯一 ID
- scons - 在一个 Scons 构建中编译多个 Godot 目标
- java - 即使我将更改后的 ArrayList 作为返回值返回,我在函数中填写的 ArrayList 也不会影响其余代码中的 ArrayList