告诉方法参数仅接受< T>作为确切的字符串 [英] Tell method's parameter to accept only <T> as exact string

查看:79
本文介绍了告诉方法参数仅接受< T>作为确切的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这样的东西:

SomeAbsClass.getSomething<IMyType>("IMyType");

所以,我该如何编码 getSomething ' s参数,以便只接受确切的 IMyType作为字符串?

So, how to I code getSomething's parameter so it'll only accept the exact "IMyType" as string?

与条件类型有一些关系,与,但我无法弄清楚。

There's something to do with Conditional types, in the same vein as this, but I can't figure it out.

推荐答案

字符串文字类型(例如 IMyType )和命名类型的名称(例如 IMyType )。字符串文字类型表示运行时存在的实际字符串值,而类型名称表示静态类型系统的各个方面,完全是 rel = nofollow noreferrer>删除

There's no inherent relationship in TypeScript between string literal types (like "IMyType") and the names of named types (like IMyType). String literal types represent actual string values which exist at runtime, while type names respresent aspects of the static type system which is completely erased from the emitted JavaScript.

此外,类型系统为结构,不是名义上的,因此类型的名称并没有多大关系。如果我声明

Furthermore, the type system is structural, not nominal, so the names of types don't really matter much. If I declare

type IYourType = IMyType;

然后 IYourType IMyType 只是同一类型的两个不同名称。因此,如果 SomeAbsClass.getSomething< IYourType>( IYourType); 成功,则 SomeAbsClass.getSomething< IMyType>( IYourType); 也应该成功。

then IYourType and IMyType are just two different names for the same type. And therefore if SomeAbsClass.getSomething<IYourType>("IYourType"); succeeds, then SomeAbsClass.getSomething<IMyType>("IYourType"); should also succeed.

没有强制性的方式来强制类型系统确定一个名称比另一个名称更规范,从而使 SomeAbsClass.getSomething< IMyType> ;( IMyType); 成功,并且 SomeAbsClass.getSomething< IMyType>( IYourType); 自动失败。

There's no principled way to force the type system to decide that one name is more "canonical" than another, so that SomeAbsClass.getSomething<IMyType>("IMyType"); succeeds and SomeAbsClass.getSomething<IMyType>("IYourType"); fails automatically.

由于这种情况不会自动发生,因此我能想到的最接近的方法是手动建立代表字符串文字和类型之间所需映射的类型,例如:

Since this doesn't happen automatically, the closest I can imagine getting here is for you to manually build up a type representing the desired mapping between string literals and types, such as:

interface TypeMapping {
    IMyType: IMyType;
    Date: Date;
    string: string;
    boolean: boolean;
    // add whatever you want
}

,然后在您对 getSomething()的定义,例如

and then use that mapping in your definition of getSomething(), like this

declare const SomeAbsClass: {
    getSomething<T>(x: {
        [K in keyof TypeMapping]: TypeMapping[K] extends T ? K : never }[keyof TypeMapping]
    ): void;
}

您这样称呼(确保明确指定泛型类型参数

And you call it like this (making sure to explicitly specify the generic type parameter when you do so):

SomeAbsClass.getSomething<IMyType>("IMyType"); // okay
SomeAbsClass.getSomething<IMyType>("IYourType"); // error

这是可行的。

好的,希望对您有所帮助。祝你好运!

Okay, hope that helps; good luck!

代码的游乐场链接

这篇关于告诉方法参数仅接受&lt; T&gt;作为确切的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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