如何将TypeScript中的Enum转换为返回keyof typeof Enum的数组?

时间:2019-08-22 11:39:45

标签: typescript

下面的枚举

export enum Types {
    Type1 = 1,
    Type2 = 2,
    ...
}

通过以下函数转换为数组

export function EnumKeys<T>(obj: object): string[] {
    return Object.keys(obj)
        .filter(value => isNaN(Number(value)) === false)
        .map(key => obj[key]);
}

EnumKeys返回string [],但我需要它返回 [typeof T的键]

1 个答案:

答案 0 :(得分:2)

(AxisFault): java.net.ProtocolException: Server redirected too many times (20) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 根据设计返回Object.keys(请参见规范问题here)。您将需要使用一种类型将string[]转换为Object.keys(obj)(我假设您想使用Array<keyof T>类型参数的obj类型)。

T

play