Since 2018, there is an easier way in Typescript, without using keyof typeof:

let obj: { [key in MyEnum]: any} =
 { [MyEnum.First]: 1, [MyEnum.Second]: 2 };

To not have to include all keys:

let obj: { [key in MyEnum]?: any} =
 { [MyEnum.First]: 1 };

To know the difference between in and keyof typeof, continue reading.


in Enum vs keyof typeof Enum

in Enum compiles to enum values and keyof typeof to enum keys.


Other differences

With keyof typeof, you cannot change the enum properties:

let obj: { [key in keyof typeof MyEnum]?: any} = { First: 1 };
obj.First = 1;
// Cannot assign to 'First' because it is a read-only property.

... unless you use -readonly:

let obj: { -readonly [key in keyof typeof MyEnum]?: any} = { First: 1 };
obj.First = 1; // works

But you can use any integer key?!:

let obj: { [key in keyof typeof MyEnum]?: any} = { First: 1 };
obj[2] = 1;

keyof typeof will compile to:

{
    [x: number]: any;
    readonly First?: any;
    readonly Second?: any;
}

Note both the [x: number] and the readonly properties. This [x: number] property doesn't exist with a string enum.

But with in Enum, you can change the object:

enum MyEnum {
    First,  // default value of this is 0
    Second, // default value of this is 1
}

let obj: { [key in  MyEnum]?: any} = { [MyEnum.First]: 1 };
obj[MyEnum.First] = 1; // can use the enum...
obj[0] = 1;            // but can also use the enum value, 
                       // as it is a numeric enum by default

It's a numeric enum. But we can't use any number:

obj[42] = 1;
// Element implicitly has an 'any' type because 
// expression of type '42' can't be used to index type '{ 0?: any; 1?: any; }'.
// Property '42' does not exist on type '{ 0?: any; 1?: any; }'.

The declaration compiles to:

{
    0?: any;
    1?: any;
}

We allow only 0 and 1, the values of the enum.

This is in line with how you would expect an enum to work, there are no surprises unlike keyof typeof.

It works with string and heterogenous enums:

enum MyEnum
{
    First = 1,
    Second = "YES"
}

let obj: { [key in  MyEnum]?: any} = { [MyEnum.First]: 1, [MyEnum.Second]: 2 };
obj[1] = 0;
obj["YES"] = 0;

Here the type is:

{
    1?: any;
    YES?: any;
}

Get immutability with readonly:

let obj: { readonly [key in MyEnum]?: any} = { 
    [MyEnum.First]: 1,
};
obj[MyEnum.First] = 2;
// Cannot assign to '1' because it is a read-only property.

... which makes these keys readonly:

{
    readonly 1?: any;
    readonly 2?: any;
}

Summary

in Enum keyof typeof Enum
Compiles to enum values Compiles to enum keys
Does not allow values outside the enum Can allow numeric values outside the enum if you use a numeric enum
Can change the object, immutability opt-in with readonly Can't change enum props without -readonly. Other numeric values outside the enum can be

Use in Enum if possible.

Answer from xy2_ on Stack Overflow
🌐
TypeScript
typescriptlang.org › docs › handbook › enums.html
TypeScript: Handbook - Enums
In this generated code, an enum is compiled into an object that stores both forward (name -> value) and reverse (value -> name) mappings. References to other enum members are always emitted as property accesses and never inlined. Keep in mind that string enum members do not get a reverse mapping ...
🌐
GitHub
github.com › Microsoft › TypeScript › issues › 16687
Using const string enum as object key produces an indexed type · Issue #16687 · microsoft/TypeScript
June 22, 2017 - CommittedThe team has roadmapped this issueThe team has roadmapped this issueFixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript ... const enum Test { A = 'a', B = 'b' } type TestMap = {[key in Test]: string} // Type ...
Author   Jessidhia
Discussions

typescript - Use enum as restricted key type - Stack Overflow
Can an enum be used as a key type instead of only number or string? Currently it seems like the only possible declaration is { [key: number]: any }, where key can be of type number or string. Is it More on stackoverflow.com
🌐 stackoverflow.com
May 29, 2017
Using string enum as key in {[key]: Type} throws not an index signature
TypeScript Version: 2.9.0-dev.20180325 Search Terms: string enum index signature cast Code enum Fruits { MANGO = 'MANGO', BANANA = 'BANANA', } type StringDict = { [key: string]: str... More on github.com
🌐 github.com
8
March 26, 2018
dictionary - How to use enum as index key type in typescript? - Stack Overflow
So if you use an index signature you can index by any number or string (we can't restrict to only DialogType keys). The concept you are using here is called mapped types. Mapped types basically generate a new type based on a union of keys (in this case the members of DialogType enum) and a set ... More on stackoverflow.com
🌐 stackoverflow.com
Allow enums as object literal keys
TypeScript Version: 2.5.3 Code enum SomeValues { first, second, } interface SomeInterface { [key: SomeValues]: string; // More on github.com
🌐 github.com
2
October 19, 2017
🌐
Futurestud.io
futurestud.io › tutorials › typescript-using-a-string-as-enum-key
TypeScript — Using a String as Enum Key - Future Studio
February 1, 2024 - This tutorial shows you how to use a string value as an enum key to access the related value. ... Enums represent a defined set of constants with specific keys and values. You can dynamically access enum values by using the related keys.
Top answer
1 of 6
445

Since 2018, there is an easier way in Typescript, without using keyof typeof:

let obj: { [key in MyEnum]: any} =
 { [MyEnum.First]: 1, [MyEnum.Second]: 2 };

To not have to include all keys:

let obj: { [key in MyEnum]?: any} =
 { [MyEnum.First]: 1 };

To know the difference between in and keyof typeof, continue reading.


in Enum vs keyof typeof Enum

in Enum compiles to enum values and keyof typeof to enum keys.


Other differences

With keyof typeof, you cannot change the enum properties:

let obj: { [key in keyof typeof MyEnum]?: any} = { First: 1 };
obj.First = 1;
// Cannot assign to 'First' because it is a read-only property.

... unless you use -readonly:

let obj: { -readonly [key in keyof typeof MyEnum]?: any} = { First: 1 };
obj.First = 1; // works

But you can use any integer key?!:

let obj: { [key in keyof typeof MyEnum]?: any} = { First: 1 };
obj[2] = 1;

keyof typeof will compile to:

{
    [x: number]: any;
    readonly First?: any;
    readonly Second?: any;
}

Note both the [x: number] and the readonly properties. This [x: number] property doesn't exist with a string enum.

But with in Enum, you can change the object:

enum MyEnum {
    First,  // default value of this is 0
    Second, // default value of this is 1
}

let obj: { [key in  MyEnum]?: any} = { [MyEnum.First]: 1 };
obj[MyEnum.First] = 1; // can use the enum...
obj[0] = 1;            // but can also use the enum value, 
                       // as it is a numeric enum by default

It's a numeric enum. But we can't use any number:

obj[42] = 1;
// Element implicitly has an 'any' type because 
// expression of type '42' can't be used to index type '{ 0?: any; 1?: any; }'.
// Property '42' does not exist on type '{ 0?: any; 1?: any; }'.

The declaration compiles to:

{
    0?: any;
    1?: any;
}

We allow only 0 and 1, the values of the enum.

This is in line with how you would expect an enum to work, there are no surprises unlike keyof typeof.

It works with string and heterogenous enums:

enum MyEnum
{
    First = 1,
    Second = "YES"
}

let obj: { [key in  MyEnum]?: any} = { [MyEnum.First]: 1, [MyEnum.Second]: 2 };
obj[1] = 0;
obj["YES"] = 0;

Here the type is:

{
    1?: any;
    YES?: any;
}

Get immutability with readonly:

let obj: { readonly [key in MyEnum]?: any} = { 
    [MyEnum.First]: 1,
};
obj[MyEnum.First] = 2;
// Cannot assign to '1' because it is a read-only property.

... which makes these keys readonly:

{
    readonly 1?: any;
    readonly 2?: any;
}

Summary

in Enum keyof typeof Enum
Compiles to enum values Compiles to enum keys
Does not allow values outside the enum Can allow numeric values outside the enum if you use a numeric enum
Can change the object, immutability opt-in with readonly Can't change enum props without -readonly. Other numeric values outside the enum can be

Use in Enum if possible.

2 of 6
140

Yes. Just type

let layer:{[key in keyof typeof MyEnum]: any}

The keyof keyword is available since Typescript 2.1. See the TypeScript documentation for more details. Using only keyof for enums wouldn't work (you'd get the keys of the enum type and not the enum constants), so you have to type keyof typeof.

🌐
DEV Community
dev.to › bwca › typing-object-keys-with-enum-values-using-typescript-4k23
Typing Object Keys With Enum Values Using Typescript - DEV Community
November 1, 2023 - interface Converter<A extends Array<unknown>, V> { <T>(keys: Array<T>, ...args: A): Record<string & T, V> } ... A an array of additional values, passed to the converted function, left as an array of unknown, since the interface does not really care about them; V the type for the values, the mapped object keys will be pointing, this will provide the flexibility, we do not impose any restrictions; T this is the enum type, which will be passed.
🌐
Richinfante
richinfante.com › 2024 › 04 › 06 › ts-enum-record-required-keys
Typescript trick: Required enum values as keys
April 6, 2024 - Instead of Record<Key1|Key2, ... are exhaustively defined as members in our object: type EnumRecord<KeyType extends string, ValueType> = {[key in KeyType]: ValueType};...
🌐
GitHub
github.com › microsoft › TypeScript › issues › 22892
Using string enum as key in {[key]: Type} throws not an index signature · Issue #22892 · microsoft/TypeScript
March 26, 2018 - TypeScript Version: 2.9.0-dev.20180325 · Search Terms: string enum index signature cast · Code · enum Fruits { MANGO = 'MANGO', BANANA = 'BANANA', } type StringDict = { [key: string]: string }; function map(dict: StringDict, transform: (key: string, value: string) => void) { const result = {}; for (const key of Object.keys(dict)) { result[key] = transform(key, dict[key]); } return result; } map(Fruits, (key, value) => { value.toLowerCase(); }); map(Fruits as StringDict, (key, value) => { value.toLowerCase(); }); Expected behavior: Both map calls succeed, because a string enum is essentially a {[key: string]: string}. I ought to be able to use it anywhere that needs something indexed by string (as long as the values are appropriate).
Author   appsforartists
Find elsewhere
🌐
GitHub
github.com › microsoft › TypeScript › issues › 19332
Allow enums as object literal keys · Issue #19332 · microsoft/TypeScript
October 19, 2017 - TypeScript Version: 2.5.3 Code enum SomeValues { first, second, } interface SomeInterface { [key: SomeValues]: string; //
Author   aminpaks
🌐
Sharooq
sharooq.com › how-to-access-keys-and-values-of-an-enum-in-typescript
How to access the keys and values of an Enum (string and numeric) in TypeScript.
July 10, 2022 - Now, let's see a numeric enum with ...NumericEnum[5]); //👈 Five ... Simply using the "Object.values()" or "Object.keys()" method will return both the keys and values in a combined array....
🌐
Technical Feeder
technicalfeeder.com › 2021 › 07 › mastering-enum-in-typescript
Typescript enum get value by key and key by value | Technical Feeder
April 12, 2023 - Explains how to get value by key, get key by value, and use an enum as an object key. Some functions don&#039;t behave as expected when an enum has number values or mixed type data. All enum-related solutions are on this page.
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-get-enum-key-by-value
Get an Enum Key by Value in TypeScript | bobbyhadz
This is why we are able to pass an enum to the Object.keys and Object.values methods. The Object.keys() method returns an array containing the keys of the enum and the Object.values() method - an array containing the enum's values.
🌐
Futurestud.io
futurestud.io › tutorials › typescript-get-all-keys-of-an-enum
TypeScript — Get All Keys of an Enum - Future Studio
January 18, 2024 - export enum FutureStudioBooks { retrofit = 'Retrofit Book', picasso = 'Picasso Book', glide = 'Glide Book', gson = 'Gson Book', 'gson-workbook' = 'Gson Workbook', } const keys = Object.keys(FutureStudioBooks) // const keys: string[] You’re not having the narrowed types from the enum’s keys. You may have expected that TypeScript knows which keys are part of the enum and narrows the returned list to only these options.
🌐
SPGuides
spguides.com › get-key-by-value-from-enum-string-in-typescript
How to Get Enum Key by Value in TypeScript?
July 30, 2025 - For TypeScript string enums, the most common approach is to use Object.keys() and Object.values() to search for the key by its value. ... enum USState { California = 'CA', Texas = 'TX', NewYork = 'NY', Florida = 'FL' } function getEnumKeyByValue<T ...
🌐
GitHub
github.com › Microsoft › TypeScript › issues › 14682
Use const enum values as keys of object literals · Issue #14682 · microsoft/TypeScript
March 16, 2017 - Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript ... Compatibility Since JS supports string literals keys, and strings containing . is not a valid id, we may limit the usage of const enums to "EnumTypeName.EnumValueName" to avoid ambiguities.
Author   gdh1995
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-enum
Enum in TypeScript
In simple words, enums allow us ... enums are number-based enums i.e. they store string values as numbers. Enums can be defined using the keyword ......
🌐
LogRocket
blog.logrocket.com › home › how to iterate over enums in typescript (with code examples)
How to iterate over enums in TypeScript (with code examples) - LogRocket Blog
June 2, 2025 - It returns a clean array of enum keys as strings, making enum key extraction straightforward and consistent across your codebase: /** * Utility to get the string keys of a TypeScript enum. * Works with both numeric and string enums. * @param enumObj The enum object * @returns Array of enum keys as strings */ function getEnumKeys<T extends object>(enumObj: T): (keyof T)[] { return Object.keys(enumObj).filter(key => isNaN(Number(key))) as (keyof T)[]; } // Example usage: enum Colors { Red = 'RED', Green = 'GREEN', Blue = 'BLUE', } const keys = getEnumKeys(Colors); console.log(keys); // Output: ['Red', 'Green', 'Blue']
🌐
DEV Community
dev.to › afrazkhan › transforming-typescript-enums-into-object-arrays-h0j
Transforming TypeScript Enums into Object Arrays - DEV Community
April 28, 2024 - Utilize the Object.values() or Object.keys() method, depending on your specific requirements. enum Scope { CREATE = "create", UPDATE = "edit", DELETE = "delete" } let Scopes = Object.values(Scope) as string[]; console.log(Scopes) // ["create", "edit", "delete"] Scopes = Object.keys(Scope) as string[]; console.log(Scopes) // ["CREATE", "UPDATE", "DELETE"]