Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type {DistributedPick} from './source/distributed-pick';
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
export type {IfEmptyObject} from './source/if-empty-object';
export type {NonEmptyObject} from './source/non-empty-object';
export type {NonEmptyString} from './source/non-empty-string';
export type {UnknownRecord} from './source/unknown-record';
export type {UnknownArray} from './source/unknown-array';
export type {Except} from './source/except';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ Click the type names for complete docs.
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true.
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types are true.
- [`NonEmptyTuple`](source/non-empty-tuple.d.ts) - Matches any non-empty tuple.
- [`NonEmptyString`](source/non-empty-string.d.ts) - Matches any non-empty string.
- [`FindGlobalType`](source/find-global-type.d.ts) - Tries to find the type of a global with the given name.
- [`FindGlobalInstanceType`](source/find-global-type.d.ts) - Tries to find one or more types from their globally-defined constructors.

Expand Down
21 changes: 21 additions & 0 deletions source/non-empty-string.d.ts
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;
11 changes: 11 additions & 0 deletions test-d/non-empty-string.ts
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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use expectType instead of expectNever.

Suggested change
declare const empty: NonEmptyString<''>;
expectNever(empty);
declare const empty: NonEmptyString<''>;
expectType<never>(empty);


declare const a: NonEmptyString<'a'>;
expectType<NonEmptyString<'a'>>(a);

declare const b: NonEmptyString<'b' | 'c'>;
expectType<NonEmptyString<'b' | 'c'>>(b);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected type and the actual type are identical, making these tests useless.

Suggested change
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);

Copy link
Copy Markdown
Collaborator

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}`>);