Skip to content

Commit

Permalink
IsLiteral: Don't restrict generic type (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa authored Sep 30, 2023
1 parent 025f6e9 commit 4378507
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 17 additions & 0 deletions source/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,20 @@ export type IsNull<T> = [T] extends [null] ? true : false;
Disallows any of the given keys.
*/
export type RequireNone<KeysType extends PropertyKey> = Partial<Record<KeysType, never>>;

/**
Returns a boolean for whether the given type is primitive value or primitive type.
@example
```
IsPrimitive<'string'>
//=> true
IsPrimitive<string>
//=> true
IsPrimitive<Object>
//=> false
```
*/
export type IsPrimitive<T> = [T] extends [Primitive] ? true : false;
7 changes: 5 additions & 2 deletions source/is-literal.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Primitive} from './primitive';
import type {Numeric} from './numeric';
import type {IsNotFalse} from './internal';
import type {IsNotFalse, IsPrimitive} from './internal';
import type {IsNever} from './is-never';

/**
Expand Down Expand Up @@ -250,4 +250,7 @@ stripLeading(str, 'abc');
@category Type Guard
@category Utilities
*/
export type IsLiteral<T extends Primitive> = IsNotFalse<IsLiteralUnion<T>>;
export type IsLiteral<T> =
IsPrimitive<T> extends true
? IsNotFalse<IsLiteralUnion<T>>
: false;

0 comments on commit 4378507

Please sign in to comment.