Typescript 我可以在泛型中使用与JS中命名参数相同的命名TS类型吗?

Typescript 我可以在泛型中使用与JS中命名参数相同的命名TS类型吗?,typescript,typescript-generics,Typescript,Typescript Generics,TS在v2.3中为泛型引入了默认参数类型 因此,我可以定义一个泛型类型,如下所示: interface MyGenericType<T = any, U = any, V = any, W = any> { } declare const xyz: MyGenericType<any, any, any, Date>; 接口MyGenericType{} 然后我可以使用它如下: interface MyGenericType<T = any, U = any,

TS在v2.3中为泛型引入了默认参数类型

因此,我可以定义一个泛型类型,如下所示:

interface MyGenericType<T = any, U = any, V = any, W = any> { }
declare const xyz: MyGenericType<any, any, any, Date>;
接口MyGenericType{}
然后我可以使用它如下:

interface MyGenericType<T = any, U = any, V = any, W = any> { }
declare const xyz: MyGenericType<any, any, any, Date>;
declare const xyz:MyGenericType;
我正在尝试摆脱这些默认类型,只指定我想按名称定义的类型,如下所示

declare const xyz: MyGenericType<{ W: Date }>;
declare const xyz:MyGenericType;
但是,由于
{W:Date}
正在替换
T
未替换
W
的第一个参数,因此该操作没有按预期工作

那件事在打字脚本中不可行吗


除了在泛型类型声明中定义
any
之外,还有其他变通方法吗?

这是一种变通方法。您可以创建另一个接口并像这样使用它

interface MyGenericType<T = any, U = any, V = any, W = any> { }

interface MyExtendedGenericType<W> extends MyGenericType<any, any, any, W> { }

declare const xyz: MyExtendedGenericType<Date>;
接口MyGenericType{}
接口MyExtendedGenericType扩展了MyGenericType{}
声明常量xyz:MyExtendedGenericType;

目前无法部分推断泛型类型参数。有关此功能的讨论可在此处找到:

在这里,您还可以找到一个现在已经结束的问题的链接,该问题讨论了按名称引用类型参数(如在您的示例中),该问题已经结束,因为这是一个相当大的更改,因为它将使类型参数的名称位于类型本身的外部,这具有相当大的含义