-
-
Notifications
You must be signed in to change notification settings - Fork 635
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 all commits
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,97 @@ | ||
import type {ApplyDefaultOptions} from './internal/object.js'; | ||
import type {UnwrapTagged, TagContainer} from './tagged.js'; | ||
import type {IsPrimitive} from './is-primitive.js'; | ||
import type {IsLiteral} from './is-literal.js'; | ||
import type {Primitive} from './primitive.js'; | ||
import type {Not} from './internal/type.js'; | ||
|
||
/** | ||
ExtactLiterals options. | ||
|
||
@see {@link ExtactLiterals} | ||
*/ | ||
export type ExtractLiteralsOptions = { | ||
/** | ||
Whether to remove infinite signature types (e.g., `abc${string}`). | ||
|
||
@default true | ||
*/ | ||
strict?: boolean; | ||
}; | ||
|
||
export type DefaultExtractLiteralsOptions = { | ||
strict: true; | ||
}; | ||
|
||
/** | ||
Creates a union of the literal members from a given union type, removing wide primitive or infinite 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}`). | ||
- **Non-strict mode (`Strict = false`)**: Removes only wide primitive types (e.g., `string`, `number`). | ||
|
||
@default true | ||
|
||
@example | ||
```ts | ||
import type { ExtractLiterals } from 'type-fest'; | ||
|
||
// String example: | ||
type Pet = LiteralUnion<'dog' | 'cat' | `${string}Dog`, string>; | ||
// ^? type Pet = 'dog' | 'cat' | `${string}Dog` | (string & {}) | ||
type PetLiteralStrict = ExtractLiterals<Pet>; | ||
// ^? type PetLiteralNonStrict = 'dog' | 'cat' | ||
type PetLiteralNonStrict = ExtractLiterals<Pet, false>; | ||
// ^? type PetLiteralStrict = 'dog' | 'cat' | `${string}Dog` | ||
|
||
// Number example: | ||
type Nums = LiteralUnion<0 | 1 | 2, number>; | ||
// ^? type Nums = 0 | 1 | 2 | (number & {}) | ||
type NumsLiteral = ExtractLiterals<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 = ExtractLiterals<Symbols>; | ||
// ^? type SymbolsLiteral = typeof sym1 | typeof sym2 | ||
|
||
// BigInt example: | ||
type Big = LiteralUnion<1n | 2n, bigint>; | ||
// ^? type Big = 1n | 2n | (bigint & {}) | ||
type BigLiteral = ExtractLiterals<Big>; | ||
// ^? type BigLiteral = 1n | 2n | ||
|
||
``` | ||
|
||
@author benzaria | ||
@see LiteralUnion | ||
@category Type | ||
*/ | ||
export type ExtractLiterals< | ||
LiteralUnion extends Primitive, | ||
Options extends ExtractLiteralsOptions = {}, | ||
> = LiteralUnion extends infer Member | ||
? (( | ||
Member extends TagContainer<any> | ||
? UnwrapTagged<Member> | ||
: Member | ||
) extends infer Type | ||
? ApplyDefaultOptions< | ||
ExtractLiteralsOptions, | ||
DefaultExtractLiteralsOptions, | ||
Options | ||
>['strict'] extends true | ||
? Not<IsLiteral<Type>> | ||
: IsPrimitive<Type> | ||
: never | ||
) extends true | ||
? never | ||
: Member | ||
: never; |
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
Oops, something went wrong.
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.