【发布时间】:2022-01-22 20:14:14
【问题描述】:
我试图在数组中获取泛型的联合类型,但只能检索泛型从实际实现中扩展的内容。
type Params = Record<string, number | string | null | undefined> | undefined;
type Route<T extends Params = undefined> = {
params: T
}
type Stack = {
routes: Route<Params>[];
}
const route1: Route<{ detailUrl: string }> = { ... };
const route2: Route<{ head: string }> = { ... };
const route3: Route = { ... };
const routeRegistry: Stack = {
routes: [route1, route2, route3]
};
type UnionOfParams = ExtractGeneric<typeof routeRegistry['routes'][number]>;
// expected: { detailUrl: string } | { head: string } | undefined
// received: Params
我知道 const 断言对于限制类型推断的扩展很有用,但在这种情况下使用它并没有产生任何结果。
我找不到与此特定问题相关的任何内容。有没有办法完成我的要求?
【问题讨论】:
-
你没有包含
ExtractGeneric
标签: typescript typescript-generics