Skip to content

Commit

Permalink
IsLiteral: Return false for tagged types (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjquines authored Jun 7, 2024
1 parent cabce98 commit 587380c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions source/is-literal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ LiteralCheck<1, string>
*/
type LiteralCheck<T, LiteralType extends Primitive> = (
IsNever<T> extends false // Must be wider than `never`
? [T] extends [LiteralType] // Must be narrower than `LiteralType`
? [LiteralType] extends [T] // Cannot be wider than `LiteralType`
? false
: true
? [T] extends [LiteralType & infer U] // Remove any branding
? [U] extends [LiteralType] // Must be narrower than `LiteralType`
? [LiteralType] extends [U] // Cannot be wider than `LiteralType`
? false
: true
: false
: false
: false
);
Expand Down
6 changes: 6 additions & 0 deletions test-d/is-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
IsNumericLiteral,
IsBooleanLiteral,
IsSymbolLiteral,
Tagged,
} from '../index';

const stringLiteral = '';
Expand Down Expand Up @@ -65,3 +66,8 @@ type A2 = IsNumericLiteral;
type A3 = IsBooleanLiteral;
// @ts-expect-error
type A4 = IsSymbolLiteral;

// Tagged types should be false
expectType<IsStringLiteral<Tagged<string, 'Tag'>>>(false);
expectType<IsNumericLiteral<Tagged<number, 'Tag'>>>(false);
expectType<IsBooleanLiteral<Tagged<boolean, 'Tag'>>>(false);

0 comments on commit 587380c

Please sign in to comment.