-
-
Notifications
You must be signed in to change notification settings - Fork 679
Add NonEmptyString type
#1103
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
Merged
sindresorhus
merged 10 commits into
sindresorhus:main
from
jor1ken:feat/non-empty-string
Apr 15, 2025
Merged
Add NonEmptyString type
#1103
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fb4f6f7
Add NonEmptyString type
jor1ken 580a8cf
Update non-empty-string.d.ts
jor1ken 355251a
Update non-empty-string.d.ts
jor1ken 54fcbb2
Update non-empty-string.d.ts
jor1ken a740ef9
Update non-empty-string.ts
jor1ken 031b5b5
style: fix line breaks
som-sm 4c7ccbf
Update non-empty-string.d.ts
sindresorhus 670c37b
update tests
jor1ken 78ef575
fix: return never for non-literal strings containing empty string
som-sm bdb54ac
Update non-empty-string.d.ts
sindresorhus 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,21 @@ | ||
| /** | ||
| Matches any non-empty string. | ||
|
|
||
| This is useful when you need a string that is not empty, for example as a function parameter. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {NonEmptyString} from 'type-fest'; | ||
|
|
||
| declare function foo<T extends string>(str: NonEmptyString<T>): void; | ||
|
|
||
| foo('a'); | ||
| //=> OK | ||
|
|
||
| foo(''); | ||
| //=> Error: Argument of type '""' is not assignable to parameter of type 'never'. | ||
| ``` | ||
|
|
||
| @category String | ||
| */ | ||
| export type NonEmptyString<T extends string> = T extends '' ? never : T; |
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,11 @@ | ||||||||||||||||||||||
| import {expectNever, expectType} from 'tsd'; | ||||||||||||||||||||||
| import type {NonEmptyString} from '../index'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| declare const empty: NonEmptyString<''>; | ||||||||||||||||||||||
| expectNever(empty); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| declare const a: NonEmptyString<'a'>; | ||||||||||||||||||||||
| expectType<NonEmptyString<'a'>>(a); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| declare const b: NonEmptyString<'b' | 'c'>; | ||||||||||||||||||||||
| expectType<NonEmptyString<'b' | 'c'>>(b); | ||||||||||||||||||||||
|
||||||||||||||||||||||
| declare const a: NonEmptyString<'a'>; | |
| expectType<NonEmptyString<'a'>>(a); | |
| declare const b: NonEmptyString<'b' | 'c'>; | |
| expectType<NonEmptyString<'b' | 'c'>>(b); | |
| declare const a: NonEmptyString<'a'>; | |
| expectType<'a'>(a); | |
| declare const b: NonEmptyString<'b' | 'c'>; | |
| expectType<'b' | 'c'>(b); |
Outdated
Collaborator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, add these test cases:
expectType<'a'>({} as NonEmptyString<'' | 'a'>);
expectType<string>({} as NonEmptyString<string>);
expectType<Uppercase<string>>({} as NonEmptyString<Uppercase<string>>);
expectType<`on${string}`>({} as NonEmptyString<`on${string}`>);
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use
expectTypeinstead ofexpectNever.