Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在TypeScript中定义联合泛型类型_Typescript - Fatal编程技术网

如何在TypeScript中定义联合泛型类型

如何在TypeScript中定义联合泛型类型,typescript,Typescript,我们可以使用: type stringNumber = string | number; 但是关于: type MapSet<T> = Map<T> | Set<T>; type MapSet=Map | Set; 有可能吗 另外,这是减少接口定义长度所必需的 更新: type MapSet<T> = Map<T> | Set<T>; type MapSet=Map | Set; 这样的定义在typeScript

我们可以使用:

type stringNumber = string | number;
但是关于:

type MapSet<T> = Map<T> | Set<T>;
type MapSet=Map | Set;
有可能吗

另外,这是减少接口定义长度所必需的

更新:

type MapSet<T> = Map<T> | Set<T>;
type MapSet=Map | Set;

这样的定义在typeScript 1.6中是正确的,您可以在今天使用它,正如Ryan在下面写的那样。

这是一个有趣的问题。下面的代码确实编译过,但必须包装每个值肯定不是最优的

interface MapSet<T> {
  wrapper: Map<T> | Set<T>;
}
接口映射集{
包装器:映射集;
}

带有泛型类型参数的类型别名在TypeScript 1.6中


您可以等待该版本发布,或者安装TypeScript nightly build(
npm install)typescript@next

很酷,现在的《夜间》有一系列功能吗?