打字稿通用联合 [英] Typescript Generic Union
问题描述
所以我有一个通用对象数组,想遍历但打字稿不允许我.这是一些示例代码.有关如何解决此问题的任何建议.
type someGeneric= {项目:T};type stringGeneric = someGeneric;type numberGeneric = someGeneric;type someFunction = <T>(泛型:someGeneric<T>)=>;const someFunction: someFunction = (generic) =>通用项目;const stringGeneric: stringGeneric = { item: 'some String' },numberGeneric: numberGeneric = { 项目: 12 };让 genericArray = [stringGeneric, numberGeneric];genericArray.forEach(generic => {someFunction(通用);//此行出错.});
您只需将代码复制粘贴到此链接.我似乎无法分享代码.
问题是如果我们尝试传入类型为 someGeneric
的参数,该函数会接受类型为 的参数代码>someGeneric<数字>|someGeneric
打字稿不会尝试从中推断 T
它只会说联合与类型 someGeneric
我们可以改变函数的定义,使类型参数扩展someGeneric
.此约束将与联合兼容.然后我们可以使用条件类型从使用条件类型的 T
中提取项目类型.由于条件类型分布在联合上,提取的结果将是泛型参数到 someGeneric
type someGeneric= {项目:T};type stringGeneric = someGeneric;type numberGeneric = someGeneric;类型 extractItemFromSomeGeneric>= T extends someGeneric<infer U>?你:从不;type someFunction = >(generic: T) =>extractItemFromSomeGeneric;const someFunction: someFunction = (generic) =>通用项目;const stringGeneric: stringGeneric = { item: 'some String' },numberGeneric: numberGeneric = { 项目: 12 };让 someGeneric = [stringGeneric, numberGeneric];someGeneric.forEach(generic => {someFunction(通用);//返回字符串 |数字});
So I have Array of generic object and want to iterate over the but typescript wont allow me. Here is some sample code. Any Suggestions of how this can be solved.
type someGeneric<T> = { item: T };
type stringGeneric = someGeneric<string>;
type numberGeneric = someGeneric<number>;
type someFunction = <T>(generic: someGeneric<T>) => T;
const someFunction: someFunction = (generic) => generic.item;
const stringGeneric: stringGeneric = { item: 'some String' },
numberGeneric: numberGeneric = { item: 12 };
let genericArray = [stringGeneric, numberGeneric];
genericArray.forEach(generic => {
someFunction(generic); // Error On This line.
});
You can just copy-paste the code to this link. I cant seem to share the code.
The problem is that the function accepts a parameter of type someGeneric<T>
if we try to pass in a parameter of type someGeneric<number> | someGeneric<string>
typescript will not try to infer T
from this it will just say the union is not compatible with the type someGeneric<T>
We can change the definition of the function so that the type parameter extends someGeneric<any>
. This constraint will be compatible with the union. We can then use a conditional type to extract the item type from the T
using a conditional type. Since conditional types distribute over unions the result of the extraction will be a union of the generic parameters to someGeneric<T>
type someGeneric<T> = { item: T };
type stringGeneric = someGeneric<string>;
type numberGeneric = someGeneric<number>;
type extractItemFromSomeGeneric<T extends someGeneric<any>> = T extends someGeneric<infer U> ? U : never;
type someFunction = <T extends someGeneric<any>>(generic: T) => extractItemFromSomeGeneric<T>;
const someFunction: someFunction = (generic) => generic.item;
const stringGeneric: stringGeneric = { item: 'some String' },
numberGeneric: numberGeneric = { item: 12 };
let someGeneric = [stringGeneric, numberGeneric];
someGeneric.forEach(generic => {
someFunction(generic); // retruns string | number
});
这篇关于打字稿通用联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!