Skip to content

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} !

@som-sm
Copy link
Collaborator

som-sm commented Sep 27, 2025

Apologies, haven't been able to re-read this. I'll give it a read sometime next week.

@benzaria
Copy link
Contributor Author

benzaria commented Oct 9, 2025

@som-sm, Hey 👋 just following up on this, you mentioned you’d review it next week, but I haven’t seen any updates yet.
No rush if you’re busy, I just wanted to check whether there’s anything else needed from my side to move this forward.

@benzaria
Copy link
Contributor Author

@som-sm Could you please take some time to review this PR when you get a chance, so we can move it forward?

@benzaria benzaria requested a review from som-sm October 21, 2025 12:27
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