简体   繁体   English

Typescript:通用参数/ReturnType 和字符串枚举有问题

[英]Typescript: Problem with generic Parameter/ReturnType and string Enums

I currently try to add proper typings to the following simplified function:我目前尝试为以下简化的 function 添加正确的类型:

getProperty(property:string):any

My goal is to make it possible to pass the property parameter as string or string enum and get the correct return type for it.我的目标是能够将属性参数作为stringstring enum传递,并为其获取正确的返回类型。

For that I have created a new string enum Property:为此,我创建了一个新的string enum属性:

enum Property {
    ENABLED = 'enabled',
    SIZE    = 'size'
}

a generic type PropertyType: generic type PropertyType:

type PropertyType < T > =
    T extends Property.ENABLED ? boolean :
    T extends Property.SIZE    ? number  : any;

and changed the function signature to:并将 function 签名更改为:

getProperty<T extends Property>(property : T) : PropertyType<T>

This works perfect if I call the function with the new string enum :如果我用新的string enum调用 function ,这将非常有效:

const enabled: boolean = getProperty(Property.ENABLED);

But if I call the function with a simple string (needed for backward compatibility) like that:但是,如果我用一个简单的字符串(向后兼容所需)调用 function,如下所示:

const enabled: boolean = getProperty("enabled");

I get the following error:我收到以下错误:

Argument of type '"enabled"' is not assignable to parameter of type 'Property'.(2345)

Is there any good way to support both ways ( string & string enum ) to pass the property parameter?有没有什么好方法可以支持两种方式( stringstring enum )来传递属性参数?

My example is also available here: Typescript Playground我的例子也可以在这里找到: Typescript Playground

You can either overload the function or make the type parameter of getProperty a union type.您可以重载function 或将getProperty的类型参数设置为联合类型。

Overload:超载:

function getProperty(property: string): any;
function getProperty<T extends Property>(property : T) : PropertyType<T>
{
    return store[property];
}

Union Type:工会类型:

function getProperty<T extends Property | string>(property : T) : PropertyType<T>
{
    return store[property];
}

Full code:完整代码:

//Example Value store
const store: { [key: string]: any } = {
    "enabled": true,
    "size": 4
};

//--------------------------------------------------------------------------

//String Enum Property
enum Property {
    ENABLED = 'enabled',
    SIZE    = 'size'
}

//--------------------------------------------------------------------------

//Property Types
type PropertyType < T > =
T extends Property.ENABLED ? boolean :
T extends Property.SIZE    ? number  : any;

//--------------------------------------------------------------------------

// Generic getProperty function
 function getProperty<T extends Property | string>(property : T) : PropertyType<T>
    {
        return store[property];
    }

//--------------------------------------------------------------------------

const enabled0: boolean = getProperty(Property.ENABLED);
const enabled1: boolean = getProperty('enabled');

Another way is to just simply choose a different function name, since you don't gain any type safety from overloading.另一种方法是简单地选择一个不同的 function 名称,因为您不会从重载中获得任何类型安全。 Imo this is a better option to stay backwards compatible. Imo 这是保持向后兼容的更好选择。

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

相关问题 通用 class 方法的 Typescript ReturnType 问题 - Typescript ReturnType problem for generic class method 通用函数的 Typescript ReturnType - Typescript ReturnType of generic function TypeScript - 通用函数的 ReturnType 不起作用 - TypeScript - ReturnType of a generic function not working 如果参数类型为“never”,typescript ReturnType 为“any” - typescript ReturnType is “any” if parameter type is “never” 函数返回函数的通用TypeScript类型,该函数将ReturnType替换为返回函数的ReturnType - Generic TypeScript Type for functions returning functions that replaces the ReturnType with the ReturnType of the returned function Typescript 枚举键入错误(“字符串”类型的参数不可分配给类型参数) - Typescript enums typing error (argument of type 'string' is not assignable to parameter of type) 将typescript ReturnType与keyof和通用类型的迭代键一起使用 - Using typescript ReturnType with keyof and iterated keys of generic type 我可以将 function(并获取其 ReturnType)传递给通用 TypeScript 类型吗? - Can I pass a function (and get its ReturnType) to a generic TypeScript type? 打字稿基于字符串的枚举 - Typescript String Based Enums 打字稿,枚举和字符串 - Typescript, enums and string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM