泛型中 [typof T] 的原因与硬编码类型具有不同的语义? [英] Reason for [typof T] in generics having different semantics that a hard coded type?

查看:24
本文介绍了泛型中 [typof T] 的原因与硬编码类型具有不同的语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在打字稿中发现了一个技巧,通过将类型映射到键值对,然后使用 keyof类型.这是一个简单的例子:

So there is a trick I found in typescript to turn an object type into a discriminated union by mapping the type to key-value pair then creating a type that can be any value in the map using the keyof type. Here is a simple example:

type SourceType =
{
    foo: number,
    bar: string
};
type MapWithKey<T> = {[P in keyof T]: { key: P, value: T[P] }}
type DescriminatedUnion = MapWithKey<SourceType>[keyof SourceType];
//The DescriminatedUnion now has the following type
DescriminatedUnion ≡ {key:"foo",value:string} | {key:"bar",value:number}

如果您想指定一个非常大的可区分联合,这非常有用,但是当您尝试使这个构造完全通用时,您最终会得到不同的类型.

This is very useful if you want to specify a very large discriminated union however when you try to make this construct fully generic you end up with a different type.

type MakeDescriminatedUnion<T> = MapWithKey<T>[keyof T];
type DescriminatedUnion = MakeDescriminatedUnion<SourceType>
//The DescriminatedUnion now has the followin type
DescriminatedUnion ≡ {key:"foo"|"bar",value:number|string}

这应该是相同的类型,但由于某种原因,它不是.我试图查看打字稿文档以找到这种行为的一些原因,但我不能.有谁知道这种差异背后的原因?或者更好的是,有人知道如何绕过这种行为并使其完全通用吗?

This should be the same type but for some reason it isn't. I have tried to look through the typescript documentation to find some reasoning for this behavior however I cannot. Does anyone know the reasoning behind this difference? Or even better does anyone know a way to get around this behavior and make it fully generic?

推荐答案

是的,这个问题 咬了 me相当 一些其他.令人惊讶的是,正如@artem 提到的,一个针对 TypeScript 2.6 的修复刚刚被引入今天!

Yes, this issue has bitten me and quite a few others. It's amazing that, as @artem mentions, a fix slated for TypeScript 2.6 was introduced just today!

与此同时,对于我们这些困在 TypeScript 2.4-land 的人来说,有一个使用 default 的解决方法泛型类型参数:

Meanwhile, for those of us stuck in TypeScript 2.4-land, there is a workaround using default generic type parameters:

type MakeDiscriminatedUnion<T, M extends MapWithKey<T> = MapWithKey<T>> = M[keyof T];
type DiscriminatedUnion = MakeDiscriminatedUnion<SourceType> // okay now

M 的实际值在您使用 MakeDiscriminatedUnion 之前不会被评估,因此编译器没有机会简化"M[keyof T] 和上面一样.

The actual value of M doesn't get evaluated until you use MakeDiscriminatedUnion<SourceType>, so the compiler has no chance to "simplify" M[keyof T] the way it does above.

无论如何,您可以选择是使用变通方法还是等待 TypeScript 2.6.希望有帮助.祝你好运!

Anyway, it's your choice whether to use the workaround or wait for TypeScript 2.6. Hope that helps. Good luck!

这篇关于泛型中 [typof T] 的原因与硬编码类型具有不同的语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆