简体   繁体   English

你能让 TS 从被赋值的值中推断出变量的泛型类型 arguments 吗?

[英]Can you make TS infer a variable's generic type arguments from those on the value being assigned?

Answering this question the solution was to specify type arguments on the Map constructor, like this:回答这个问题的解决方案是在Map构造函数上指定类型 arguments ,如下所示:

const conditions3: ReadonlyMap<string, any> = new Map<string, any>([
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^
    [FirstName.Alex, 'foo'],
    [Lastname.Smith, 'bar']
]);

Note that the OP wanted to give conditions3 the type ReadonlyMap<string, any> , not just a Map<string, any> ;请注意,OP 想要给conditions3类型ReadonlyMap<string, any> ,而不仅仅是Map<string, any> otherwise we could have just removed the type annotation from conditions3 entirely.否则我们可以完全从conditions3 3 中删除类型注释。

Unfortunately, that means repeating the type arguments on both ReadonlyMap and Map .不幸的是,这意味着在ReadonlyMapMap上重复 arguments 类型。

In the general case, is there a way to tell TypeScript to infer the type arguments for the variable/const's type ( ReadonlyMap in this example) from the type arguments on the value being assigned ( Map in this example)? In the general case, is there a way to tell TypeScript to infer the type arguments for the variable/const's type ( ReadonlyMap in this example) from the type arguments on the value being assigned ( Map in this example)? I don't mean solutions to this specific case (I think I'd probably have a function, perhaps one that literally provided a read-only map, at least in development builds), but a general infer-from-the-target solution?我的意思不是针对这种特定情况的解决方案(我想我可能会有一个 function,也许它实际上提供了一个只读的 map,至少在开发版本中),而是一个通用的从目标推断的解决方案?

My various naive approaches don't work (the first two inspired by Java's <> ):我的各种幼稚方法都不起作用(前两种方法受 Java <>的启发):

const conditions3: ReadonlyMap<> = new Map<string, any>([
//                 ^^^^^^^^^^^^^−−−−− Generic type 'ReadonlyMap<K, V>' requires
//                                    2 type argument(s).(2314)
    [FirstName.Alex, 'foo'],
    [Lastname.Smith, 'bar']
]);

playground link 游乐场链接

const conditions3: ReadonlyMap<string, any> = new Map<>([
// Same error with no matching overloads the OP had −^^
    [FirstName.Alex, 'foo'],
    [Lastname.Smith, 'bar']
]);

playground link 游乐场链接

const conditions3: ReadonlyMap = new Map<string, any>([
//                 ^^^^^^^^^^^−−−−− Generic type 'ReadonlyMap<K, V>' requires
//                                  2 type argument(s).(2314)
    [FirstName.Alex, 'foo'],
    [Lastname.Smith, 'bar']
]);

playground link 游乐场链接

Is the repetition avoidable in the general case?在一般情况下可以避免重复吗?

Here's the setup code (so it's in the question, not just linked):这是设置代码(所以它在问题中,而不仅仅是链接):

export enum FirstName {
    Alex = 'alex',
    Bob = 'bob'
}
    
export enum Lastname {
    Smith = 'smith',
    Abrams = 'abrams'
} 

There is no syntax for partial inference in variables unfortunately.不幸的是,变量中没有用于部分推断的语法。

The only work around would be to use a function as you mention in the comments:正如您在评论中提到的那样,唯一的解决方法是使用 function :

function roMap<Key, Value>(map: Map<Key, Value>): ReadonlyMap<Key, Value> { return map; } 

const conditions3 = roMap(new Map<string, any>([
    [FirstName.Alex, 'foo'],
    [Lastname.Smith, 'bar']
]));

Playground Link 游乐场链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM