-
-
Notifications
You must be signed in to change notification settings - Fork 644
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
benzaria
wants to merge
24
commits into
sindresorhus:main
Choose a base branch
from
benzaria:UnionToEnum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+277
−0
Open
Add UnionToEnum type
#1171
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a625258
Add `UnionToEnum` type and corresponding tests, and docs
benzaria c338ab4
Update example in `UnionToEnum` type to use more descriptive keys
benzaria a910dcb
Improve documentation for UnionToEnumOptions and related types
benzaria 02f1a93
Add 'as const' to tests for `readonly` keyword
benzaria ef81023
feat: remove case transformation
benzaria ed6350d
Merge branch 'main' into UnionToEnum
benzaria 767dbf9
Merge branch 'main' into UnionToEnum
benzaria 40e029f
doc: add description in `readme`
benzaria e763095
doc: sanitize JsDoc & description
benzaria d54d526
feat: add extra Edge cases in tests
benzaria 030ce52
feat: move `Numeric` to options, improve options JsDoc & add new egde…
benzaria eb8b785
Merge branch 'main' into UnionToEnum
benzaria e603f66
Merge branch 'main' into UnionToEnum
benzaria 2471aef
Update union-to-enum.d.ts
sindresorhus b733afd
update docs
benzaria ba20563
Merge branch 'main' into UnionToEnum
benzaria d64d391
add `export {}` to EOL
benzaria dae00cf
Merge branch 'main' into UnionToEnum
benzaria 7167d17
Update union-to-enum.d.ts
sindresorhus c7bad80
add more tests
benzaria 7efbf20
fix: eliminate the possibility of passing a union and tuple keys at t…
benzaria bc8d651
Merge branch 'origin/UnionToEnum' into UnionToEnum
benzaria fcc9a08
docs: fix JsDoc examples
benzaria 151e08b
add some test covering changes
benzaria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import type {ApplyDefaultOptions} from './internal/object.d.ts'; | ||
| import type {UnionToTuple} from './union-to-tuple.d.ts'; | ||
| import type {UnknownArray} from './unknown-array.d.ts'; | ||
| import type {IsLiteral} from './is-literal.d.ts'; | ||
| import type {Simplify} from './simplify.d.ts'; | ||
| import type {IsNever} from './is-never.d.ts'; | ||
| import type {Sum} from './sum.d.ts'; | ||
|
|
||
| /** | ||
| {@link UnionToEnum} Options. | ||
| */ | ||
| type UnionToEnumOptions = { | ||
| /** | ||
| The first numeric value to assign when using numeric indices. | ||
|
|
||
| @default 1 | ||
|
|
||
| @example | ||
| ``` | ||
| type E1 = UnionToEnum<['Play', 'Pause', 'Stop'], {numeric: true}>; | ||
| //=> { Play: 1; Pause: 2; Stop: 3 } | ||
|
|
||
| type E2 = UnionToEnum<['Play', 'Pause', 'Stop'], {numeric: true; startIndex: 3}>; | ||
| //=> { Play: 3; Pause: 4; Stop: 5 } | ||
|
|
||
| type E3 = UnionToEnum<['Play', 'Pause', 'Stop'], {numeric: true; startIndex: -1}>; | ||
| //=> { Play: -1; Pause: 0; Stop: 1 } | ||
| ``` | ||
| */ | ||
| startIndex?: number; | ||
| /** | ||
| Whether to use numeric indices as values. | ||
|
|
||
| @default false | ||
|
|
||
| @example | ||
| ``` | ||
| type E1 = UnionToEnum<'X' | 'Y' | 'Z'>; | ||
| //=> { X: 'X'; Y: 'Y'; Z: 'Z' } | ||
|
|
||
| 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 } | ||
| ``` | ||
| */ | ||
| numeric?: boolean; | ||
| }; | ||
|
|
||
| type DefaultUnionToEnumOptions = { | ||
| startIndex: 1; | ||
| numeric: false; | ||
| }; | ||
|
|
||
| /** | ||
| Converts a union or tuple of property keys (string, number, or symbol) into an **Enum**. | ||
|
|
||
benzaria marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The keys are preserved, and their values are either: | ||
|
|
||
| - Their own literal values (by default) | ||
| - Or numeric indices (`1`, `2`, ...) if {@link UnionToEnumOptions.numeric `numeric`} is `true`. Union ordering is not guaranteed. | ||
|
|
||
| By default, **numeric Enums** start from **Index `1`**. See {@link UnionToEnumOptions.startIndex `startIndex`} to change this behavior. | ||
|
|
||
| This is useful for creating strongly typed enums from a union/tuple of literals. | ||
|
|
||
| @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>; | ||
| //=> {} | ||
| ``` | ||
|
|
||
| @example | ||
| ``` | ||
| import type {UnionToEnum, CamelCasedProperties} from 'type-fest'; | ||
|
|
||
| const verb = ['write', 'read', 'delete'] as const; | ||
| const resource = ['file', 'folder', 'link'] as const; | ||
|
|
||
| declare function createEnum< | ||
| const T extends readonly string[], | ||
| const U extends readonly string[], | ||
| >(x: T, y: U): CamelCasedProperties<UnionToEnum<`${T[number]}_${U[number]}`>>; | ||
|
|
||
| const Template = createEnum(verb, resource); | ||
| //=> { | ||
| // writeFile: 'write_file', | ||
| // writeFolder: 'write_folder', | ||
| // writeLink: 'write_link', | ||
| // readFile: 'read_file', | ||
| // readFolder: 'read_folder', | ||
| // readLink: 'read_link', | ||
| // deleteFile: 'delete_file', | ||
| // deleteFolder: 'delete_folder', | ||
| // deleteLink: 'delete_link', | ||
| // } | ||
| ``` | ||
|
|
||
| @see UnionToTuple | ||
| @category Object | ||
| */ | ||
| export type UnionToEnum< | ||
| Keys extends ( | ||
| [Keys] extends [PropertyKey] | ||
| ? PropertyKey | ||
| : readonly PropertyKey[] | ||
| ), | ||
| Options extends UnionToEnumOptions = {}, | ||
| > = IsNever<Keys> extends true ? {} | ||
| : _UnionToEnum< | ||
| [Keys] extends [UnknownArray] ? Keys : UnionToTuple<Keys>, | ||
| ApplyDefaultOptions<UnionToEnumOptions, DefaultUnionToEnumOptions, Options> | ||
| >; | ||
|
|
||
| /** | ||
| Core type for {@link UnionToEnum}. | ||
| */ | ||
| type _UnionToEnum< | ||
| Keys extends UnknownArray, | ||
| Options extends Required<UnionToEnumOptions>, | ||
| > = Simplify<{readonly [ | ||
| K in keyof Keys as K extends `${number}` | ||
| ? Keys[K] extends PropertyKey | ||
| ? IsLiteral<Keys[K]> extends true // TODO: update to accept template literals | ||
| ? Keys[K] | ||
| : never // Not a literal | ||
| : never // Not a property key | ||
| : never // Not an index | ||
| ]: Options['numeric'] extends true | ||
| ? K extends `${infer N extends number}` | ||
| ? Sum<N, Options['startIndex']> | ||
| : never // Not an index | ||
| : Keys[K] | ||
| }>; | ||
|
|
||
| export {}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import {expectType} from 'tsd'; | ||
| import type {CamelCasedProperties, UnionToEnum} from '../index.d.ts'; | ||
|
|
||
| // Union input | ||
| expectType<UnionToEnum<'b' | 'a' | 'd' | 'c'>>({b: 'b', a: 'a', d: 'd', c: 'c'} as const); | ||
| expectType<UnionToEnum<3 | 2 | 4 | 1>>({1: 1, 2: 2, 3: 3, 4: 4} as const); | ||
|
|
||
| // Tuple input | ||
| expectType<UnionToEnum<['One', 'Two']>>({One: 'One', Two: 'Two'} as const); | ||
| expectType<UnionToEnum<['X', 'Y', 'Z'], {numeric: true}>>({X: 1, Y: 2, Z: 3} as const); | ||
| expectType<UnionToEnum<['A', 'B'] | ['C', 'D']>>({} as Readonly<{A: 'A'; B: 'B'} | {C: 'C'; D: 'D'}>); | ||
| expectType<UnionToEnum<['A', 'B'] | ['C', 'D'], {numeric: true}>>({} as Readonly<{A: 1; B: 2} | {C: 1; D: 2}>); | ||
|
|
||
| // Single element tuple | ||
| expectType<UnionToEnum<['Only']>>({Only: 'Only'} as const); | ||
| expectType<UnionToEnum<'Only', {numeric: true}>>({Only: 1} as const); | ||
| expectType<UnionToEnum<[0]>>({0: 0} as const); | ||
| expectType<UnionToEnum<[0], {numeric: true; startIndex: 5}>>({0: 5} as const); | ||
|
|
||
| // Tuple with numeric keys | ||
| expectType<UnionToEnum<[1, 2, 3]>>({1: 1, 2: 2, 3: 3} as const); | ||
| expectType<UnionToEnum<[1, 2, 3], {numeric: true; startIndex: 10}>>({1: 10, 2: 11, 3: 12} as const); | ||
|
|
||
| // Mixed keys | ||
| expectType<UnionToEnum<['a', 1, 'b']>>({a: 'a', 1: 1, b: 'b'} as const); | ||
| expectType<UnionToEnum<['a', 1, 'b'], {numeric: true; startIndex: 0}>>({a: 0, 1: 1, b: 2} as const); | ||
|
|
||
| // Symbol keys | ||
| declare const sym1: unique symbol; | ||
| declare const sym2: unique symbol; | ||
|
|
||
| expectType<UnionToEnum<typeof sym1 | typeof sym2>>({[sym1]: sym1, [sym2]: sym2} as const); | ||
| expectType<UnionToEnum<[typeof sym1, typeof sym2]>>({[sym1]: sym1, [sym2]: sym2} as const); | ||
|
|
||
| // Unordered union with numeric flag | ||
| expectType<UnionToEnum<'left' | 'right' | 'up' | 'down', {numeric: true}>>({left: 1, right: 2, up: 3, down: 4} as const); | ||
|
|
||
| // Large union | ||
| type BigUnion = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g'; | ||
| expectType<UnionToEnum<BigUnion>>({a: 'a', b: 'b', c: 'c', d: 'd', e: 'e', f: 'f', g: 'g'} as const); | ||
|
|
||
| // Symbols with numeric indices | ||
| expectType<UnionToEnum<[typeof sym1, typeof sym2], {numeric: true}>>({[sym1]: 1, [sym2]: 2} as const); | ||
|
|
||
| // Readonly tuple input | ||
| expectType<UnionToEnum<readonly ['a', 'b', 'c']>>({a: 'a', b: 'b', c: 'c'} as const); | ||
|
|
||
| // Non-literal input fallback | ||
| expectType<UnionToEnum<string>>({}); | ||
| expectType<UnionToEnum<number>>({}); | ||
| expectType<UnionToEnum<symbol>>({}); | ||
| expectType<UnionToEnum<string[]>>({}); | ||
| expectType<UnionToEnum<number[]>>({}); | ||
| expectType<UnionToEnum<symbol[]>>({}); | ||
| expectType<UnionToEnum<[string]>>({}); | ||
| expectType<UnionToEnum<number | string>>({}); | ||
| expectType<UnionToEnum<[string, 'foo']>>({foo: 'foo'} as const); | ||
| expectType<UnionToEnum<`foo${string}` | 'bar'>>({bar: 'bar'} as const); | ||
|
|
||
| // Empty array cases | ||
| expectType<UnionToEnum<[]>>({}); | ||
| expectType<UnionToEnum<readonly []>>({}); | ||
|
|
||
| // `never` / `any` | ||
| expectType<UnionToEnum<never>>({}); | ||
| expectType<UnionToEnum<any>>({}); | ||
|
|
||
| // Literal const arrays | ||
| const buttons = ['Play', 'Pause', 'Stop'] as const; | ||
|
|
||
| expectType<UnionToEnum<typeof buttons>>({Play: 'Play', Pause: 'Pause', Stop: 'Stop'} as const); | ||
| expectType<UnionToEnum<typeof buttons, {numeric: true; startIndex: 0}>>({Play: 0, Pause: 1, Stop: 2} as const); | ||
|
|
||
| const level = ['DEBUG', 'INFO', 'ERROR', 'WARNING'] as const; | ||
|
|
||
| expectType<UnionToEnum<typeof level>>({ | ||
| DEBUG: 'DEBUG', | ||
| INFO: 'INFO', | ||
| ERROR: 'ERROR', | ||
| WARNING: 'WARNING', | ||
| } as const); | ||
| expectType<UnionToEnum<typeof level, {numeric: true}>>({ | ||
| DEBUG: 1, | ||
| INFO: 2, | ||
| ERROR: 3, | ||
| WARNING: 4, | ||
| } as const); | ||
|
|
||
| // Dynamic Enum | ||
| const verb = ['write', 'read', 'delete'] as const; | ||
| const resource = ['file', 'folder', 'link'] as const; | ||
|
|
||
| declare function createEnum< | ||
| const T extends readonly string[], | ||
| const U extends readonly string[], | ||
| >(x: T, y: U): CamelCasedProperties<UnionToEnum<`${T[number]}_${U[number]}`>>; | ||
|
|
||
| const Template = createEnum(verb, resource); | ||
|
|
||
| expectType<typeof Template>({ | ||
| writeFile: 'write_file', | ||
| writeFolder: 'write_folder', | ||
| writeLink: 'write_link', | ||
| readFile: 'read_file', | ||
| readFolder: 'read_folder', | ||
| readLink: 'read_link', | ||
| deleteFile: 'delete_file', | ||
| deleteFolder: 'delete_folder', | ||
| deleteLink: 'delete_link', | ||
| } as const); | ||
benzaria marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Edge cases for startIndex | ||
| expectType<UnionToEnum<['x'], {numeric: true; startIndex: -1}>>({x: -1} as const); | ||
| expectType<UnionToEnum<['x', 'y'], {numeric: true; startIndex: -100}>>({x: -100, y: -99} as const); | ||
| expectType<UnionToEnum<['test'], {numeric: true; startIndex: 100}>>({test: 100} as const); | ||
|
|
||
| // Numeric edge cases | ||
| expectType<UnionToEnum<0 | -1 | 42>>({0: 0, [-1]: -1, 42: 42} as const); | ||
| expectType<UnionToEnum<[0, -5, 999], {numeric: true}>>({0: 1, [-5]: 2, 999: 3} as const); | ||
benzaria marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // @ts-expect-error no mixed input | ||
| type T = UnionToEnum<'A' | ['B']>; | ||
| type V = UnionToEnum<['A'] | ['B']>; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.