Skip to content
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

IsStringLiteral: add new util #562

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -101,6 +101,7 @@ export type {Replace} from './source/replace';
export type {Includes} from './source/includes';
export type {Get} from './source/get';
export type {LastArrayElement} from './source/last-array-element';
export type {IsStringLiteral} from './source/is-string-literal';

// Miscellaneous
export type {GlobalThis} from './source/global-this';
Expand Down
23 changes: 23 additions & 0 deletions source/is-string-literal.d.ts
@@ -0,0 +1,23 @@
/**
Returns a boolean for whether a given string is a literal.

@link https://github.com/sindresorhus/type-fest/issues/541

Use-cases:
- If you want to make sure that a given type is an explicit literal string, and not a generic string

@example
```
import type {IsStringLiteral} from 'type-fest';

IsStringLiteral<'red'>; // true
IsStringLiteral<string>; // false
```

@category Template literal
*/
export type IsStringLiteral<T> = T extends string
? string extends T
? false
: true
: false;
10 changes: 10 additions & 0 deletions test-d/is-string-literal.ts
@@ -0,0 +1,10 @@
import {expectType} from 'tsd';
import type {IsStringLiteral} from '../index';

declare const test1: IsStringLiteral<'hello'>;
declare const test2: IsStringLiteral<string>;
declare const test3: IsStringLiteral<number>;

expectType<true>(test1);
expectType<false>(test2);
expectType<false>(test3);