-
-
Notifications
You must be signed in to change notification settings - Fork 655
Add ExtractLiterals and IsPrimitive types
#1145
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
Draft
benzaria
wants to merge
10
commits into
sindresorhus:main
Choose a base branch
from
benzaria:merge-conflict/main
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.
Draft
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aab1e00
Add `LoosenUnion` type
benzaria 0dcc5c6
Remove `LoosenUnion` type in favor of `LiteralOf`
benzaria 91b9164
Add `IsPrimitive` types
benzaria f9a62f0
Improve `IsLiteral` types
benzaria 9cc1c08
Add `Extends` type from `expect-type`
benzaria 2bc33be
Add `ExtractLiterals` type
benzaria 8578cd4
Add `ExtractLiterals` and `IsPrimitive` types
benzaria f416c34
Merge branch 'main' into merge-conflict/main
benzaria 96b1e17
Fix conflict with recent changes on is-literal
benzaria a7995c5
Adding `ExtractLiterals` and `IsPrimitive` types to `docs`
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,93 @@ | ||
| import type {UnwrapTagged, TagContainer} from './tagged.js'; | ||
| import type {Primitive} from './primitive.d.ts'; | ||
| import type {IsLiteral} from './is-literal.js'; | ||
| import type {Not} from './internal/type.d.ts'; | ||
| import type {IsEqual} from './is-equal.d.ts'; | ||
| import type {ValueOf} from './value-of.d.ts'; | ||
|
|
||
| /** | ||
| Create a union by removing all Non-Literal primitive types from a union, while retaining literal types. | ||
|
|
||
| This utility helps you extract only the literal members from a "literal union" type | ||
| (e.g., `'foo' | 'bar' | string` becomes `'foo' | 'bar'`), saving you from defining separate types for literals and unions. | ||
|
|
||
| It works with all primitive and tagged types, and supports two strictness modes: | ||
|
|
||
| - **Strict mode (`Strict = true`)**: Removes any infinite signature type (e.g., `abc${string}`, `123${number}`), keeping only genuine literals. | ||
| - **Non-strict mode (`Strict = false`)**: Removes only wide primitive types (e.g., `string`, `number`). | ||
|
|
||
| @default false | ||
|
|
||
| @example | ||
| ```ts | ||
| import type { LoosenUnion } from 'type-fest'; | ||
|
|
||
| // String example: | ||
| type Pet = LiteralUnion<'dog' | 'cat' | `${string}Dog`, string>; | ||
| // ^? type Pet = 'dog' | 'cat' | `${string}Dog` | (string & {}) | ||
| type PetLiteralNonStrict = LoosenUnion<Pet>; | ||
| // ^? type PetLiteralNonStrict = 'dog' | 'cat' | `${string}Dog` | ||
| type PetLiteralStrict = LoosenUnion<Pet, true>; | ||
| // ^? type PetLiteralStrict = 'dog' | 'cat' | ||
|
|
||
| // Number example: | ||
| type Nums = LiteralUnion<0 | 1 | 2, number>; | ||
| // ^? type Nums = 0 | 1 | 2 | (number & {}) | ||
| type NumsLiteral = LoosenUnion<Nums>; | ||
| // ^? type NumsLiteral = 0 | 1 | 2 | ||
|
|
||
| // Symbol example: | ||
| declare const sym1: unique symbol; | ||
| declare const sym2: unique symbol; | ||
| type Symbols = LiteralUnion<typeof sym1 | typeof sym2, symbol>; | ||
| // ^? type Symbols = typeof sym1 | typeof sym2 | (symbol & {}) | ||
| type SymbolsLiteral = LoosenUnion<Symbols>; | ||
| // ^? type SymbolsLiteral = typeof sym1 | typeof sym2 | ||
|
|
||
| // BigInt example: | ||
| type Big = LiteralUnion<1n | 2n, bigint>; | ||
| // ^? type Big = 1n | 2n | (bigint & {}) | ||
| type BigLiteral = LoosenUnion<Big>; | ||
| // ^? type BigLiteral = 1n | 2n | ||
|
|
||
| ``` | ||
|
|
||
| @author benzaria | ||
| @see LiteralUnion | ||
| @category Type | ||
| */ | ||
| export type LoosenUnion< | ||
| LiteralUnion extends Primitive, | ||
| Strict extends boolean = false, | ||
| > = ValueOf<{ | ||
| [P in Primitive as string]: LiteralUnion extends P | ||
| ? _LoosenUnion<LiteralUnion, P, Strict> | ||
| : never | ||
| }>; | ||
|
|
||
| type _LoosenUnion<U, P, S> = ValueOf<{ | ||
| [K in U as string]: ( | ||
| S extends true | ||
| ? Not<IsLiteral< | ||
| K extends TagContainer<{[J in PropertyKey]: any}> | ||
| ? UnwrapTagged<K> | ||
| : K | ||
| >> | ||
| : IsEqual<K, (P & Record<never, never>)> | ||
| ) extends true | ||
| ? never | ||
| : K | ||
| }>; | ||
|
|
||
| /** | ||
|
|
||
| ? Explanation: | ||
|
|
||
| - LoosenUnion iterates over all primitive types (string, number, symbol, bigint, boolean). | ||
| - For each, if the input type extends that primitive, applies _LoosenUnion. | ||
| - _LoosenUnion iterates over the union members: | ||
| - In non-strict mode, removes (type & {}) (the canonical "widened" type). | ||
| - In strict mode, removes any non-literal (infinite template literal types). | ||
| - Otherwise, keeps the literal member. | ||
|
|
||
| */ |
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,71 @@ | ||
| import {expectType} from 'tsd'; | ||
| import type {LoosenUnion, LiteralUnion, IsEqual, Tagged} from '../index.d.ts'; | ||
|
|
||
| // For Strings: | ||
|
|
||
| type Events = LiteralUnion<'hover' | 'click' | `click-${number}`, string>; | ||
| // ^? | ||
| type EventsLiteralNonStrict = LoosenUnion<Events>; | ||
| // ^? | ||
| type EventsLiteralStrict = LoosenUnion<Events, true>; | ||
| // ^? | ||
|
|
||
| expectType<IsEqual<'hover' | 'click' | `click-${number}`, EventsLiteralNonStrict>>(true); | ||
| expectType<IsEqual<'hover' | 'click', EventsLiteralStrict>>(true); | ||
|
|
||
| expectType<IsEqual<'hover' | 'click' | `click-${number}`, EventsLiteralStrict>>(false); | ||
| expectType<IsEqual<'hover' | 'click', EventsLiteralNonStrict>>(false); | ||
|
|
||
| // For Numbers: | ||
|
|
||
| type Nums = LiteralUnion<0 | 1 | 2, number>; | ||
| // ^? | ||
| type NumsLiteral = LoosenUnion<Nums>; | ||
| // ^? | ||
|
|
||
| expectType<IsEqual<0 | 1 | 2, NumsLiteral>>(true); | ||
| expectType<IsEqual<0 | 1 | 2, Nums>>(false); | ||
|
|
||
| // For Symbols: | ||
|
|
||
| declare const symbol1: unique symbol; | ||
| declare const symbol2: unique symbol; | ||
|
|
||
| type Symbol1 = typeof symbol1; | ||
| type Symbol2 = typeof symbol2; | ||
|
|
||
| type Symbols = LiteralUnion<Symbol1 | Symbol2, symbol>; | ||
| // ^? | ||
| type SymbolsLiteral = LoosenUnion<Symbols>; | ||
| // ^? | ||
|
|
||
| expectType<IsEqual<Symbol1 | Symbol2, SymbolsLiteral>>(true); | ||
| expectType<IsEqual<Symbol1 | Symbol2, Symbols>>(false); | ||
|
|
||
| // For Bigints: | ||
|
|
||
| type Big = LiteralUnion<1n | 2n, bigint>; | ||
| // ^? | ||
| type BigLiteral = LoosenUnion<Big>; | ||
| // ^? | ||
|
|
||
| expectType<IsEqual<1n | 2n, BigLiteral>>(true); | ||
| expectType<IsEqual<1n | 2n, Big>>(false); | ||
|
|
||
| // For Tagged types: | ||
|
|
||
| type Animals = Tagged<'dog' | 'cat' | `${string}Dog`, 'alimals'>; | ||
| type AnimalsStrict = Exclude<Animals, `${string}Dog`>; | ||
|
|
||
| type TaggedUnion = LiteralUnion<Animals, string>; | ||
| // ^? | ||
| type TaggedLiteral = LoosenUnion<TaggedUnion>; | ||
| // ^? | ||
| type TaggedStrict = LoosenUnion<TaggedUnion, true>; | ||
| // ^? | ||
|
|
||
| expectType<IsEqual<Animals, TaggedLiteral>>(true); | ||
| expectType<IsEqual<AnimalsStrict, TaggedStrict>>(true); | ||
|
|
||
| expectType<IsEqual<Animals, TaggedUnion>>(false); | ||
| expectType<IsEqual<AnimalsStrict, TaggedUnion>>(false); |
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.