Skip to content

Add UnionToEnum type #1171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Add UnionToEnum type #1171

wants to merge 12 commits into from

Conversation

benzaria
Copy link
Contributor

@benzaria benzaria commented Jun 7, 2025

Closes #1084

Add UnionToEnum<T, Numeric, Options> utility for converting a union/tuple into an Enum-like object.

  • string, number, or symbol unions or tuples
  • Numeric Enum values starting from configurable Index, default: 1

Example:

import type {UnionToEnum} from 'type-fest';

type E1 = UnionToEnum<'A' | 'B' | 'C'>;
//=> { A: 'A'; B: 'B'; C: 'C' }

type E2 = UnionToEnum<'X' | 'Y' | 'Z', {numeric: true}>;
//=> { X: 1; Y: 2; Z: 3 }

type E3 = UnionToEnum<['Play', 'Pause', 'Stop'], {numeric: true; startIndex: 3}>;
//=> { Play: 3; Pause: 4; Stop: 5 }

type E4 = UnionToEnum<['some_key', 'another_key']>;
//=> { 'some_key': 'some_key'; 'another_key': 'another_key' }

type E5 = UnionToEnum<never>;
//=> {}

@benzaria
Copy link
Contributor Author

benzaria commented Jun 7, 2025

@som-sm @sindresorhus Should CamelCase be true by default ?

@sindresorhus
Copy link
Owner

This type is doing too much. It should not do case transformation. That would be a task for a separate type. It should purely transform union to enum.

@benzaria
Copy link
Contributor Author

benzaria commented Jun 9, 2025

This type is doing too much. It should not do case transformation. That would be a task for a separate type. It should purely transform union to enum.

fair enough i will remove case transformation.

@sindresorhus
Copy link
Owner

I don't think it should do startingIndex either. It's too limiting and could be done by a separate type too.

@benzaria
Copy link
Contributor Author

benzaria commented Jun 9, 2025

I don't think it should do startingIndex either. It's too limiting and could be done by a separate type too.

Should I keep the starting index 1 or 0 ?

@sindresorhus
Copy link
Owner

Hmm, didn't think of that. Maybe we do indeed need the startIndex.

@benzaria
Copy link
Contributor Author

benzaria commented Jun 9, 2025

Hmm, didn't think of that. Maybe we do indeed need the startIndex.

True, diff users may want diff index for there enum.

@benzaria
Copy link
Contributor Author

@sindresorhus @som-sm Ready !

Copy link
Collaborator

@som-sm som-sm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should be the behaviour in cases where UnionToEnum returns an index signature?

For example, currently invalid index signatures are returned in cases like these:

type T1 = UnionToEnum<string | number>;
// {readonly [x: string]: string; readonly [x: number]: number}

type T2 = UnionToEnum<[string, 'foo'], true>;
// {readonly [x: string]: 1; readonly foo: 2}

image

Maybe we should skip all non-literals?

@benzaria
Copy link
Contributor Author

What should be the behaviour in cases where UnionToEnum returns an index signature?

Should also skip foo${string} !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Union to Enum
3 participants