Skip to content

Commit 07e76cd

Browse files
committed
Fix conflict with recent changes on is-literal
1 parent f416c34 commit 07e76cd

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

source/is-literal.d.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type {CollapseLiterals, Extends, IsNotFalse, Not} from './internal/type.d.ts';
1+
import type {Extends, IsNotFalse, Not} from './internal/type.d.ts';
2+
import type {CollapseLiterals} from './internal/object.d.ts';
23
import type {TagContainer, UnwrapTagged} from './tagged.js';
34
import type {Primitive} from './primitive.d.ts';
45
import type {IsNever} from './is-never.d.ts';
56
import type {Numeric} from './numeric.d.ts';
67
import type {And} from './and.js';
78

8-
99
/**
1010
Returns a boolean for whether the given type `T` is the specified `LiteralType`.
1111
@@ -121,14 +121,15 @@ export type IsStringLiteral<T> = IsNever<T> extends false
121121
? _IsStringLiteral<CollapseLiterals<T extends TagContainer<any> ? UnwrapTagged<T> : T>>
122122
: false;
123123

124-
export type _IsStringLiteral<S> =
125-
// If `T` is an infinite string type (e.g., `on${string}`), `Record<T, never>` produces an index signature,
126-
// and since `{}` extends index signatures, the result becomes `false`.
127-
S extends string
128-
? {} extends Record<S, never>
129-
? false
130-
: true
131-
: false;
124+
export type _IsStringLiteral<S> = (
125+
// If `T` is an infinite string type (e.g., `on${string}`), `Record<T, never>` produces an index signature,
126+
// and since `{}` extends index signatures, the result becomes `false`.
127+
S extends string
128+
? {} extends Record<S, never>
129+
? false
130+
: true
131+
: false
132+
);
132133

133134
/**
134135
Returns a boolean for whether the given type is a `number` or `bigint` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
@@ -177,11 +178,9 @@ endsWith('abc123', end);
177178
@category Type Guard
178179
@category Utilities
179180
*/
180-
export type IsNumericLiteral<T> = (
181-
T extends Numeric
182-
? LiteralChecks<T, Numeric>
183-
: false
184-
);
181+
export type IsNumericLiteral<T> = T extends Numeric
182+
? LiteralChecks<T, Numeric>
183+
: false;
185184

186185
/**
187186
Returns a boolean for whether the given type is a `true` or `false` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).

source/is-primitive.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ type ex = IsNumericPrimitive<1>
104104
@category Type Guard
105105
@category Utilities
106106
*/
107-
export type IsNumericPrimitive<T> = (
108-
T extends Numeric
109-
? PrimitiveChecks<T, Numeric>
110-
: false
111-
);
107+
export type IsNumericPrimitive<T> = T extends Numeric
108+
? PrimitiveChecks<T, Numeric>
109+
: false;
112110

113111
/**
114112
Returns a boolean for whether the given type is strictly a `boolean` [primitive type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#the-primitives-string-number-and-boolean).

test-d/is-literal.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,19 @@ expectType<IsBooleanLiteral<Tagged<boolean, 'Tag'>>>(false);
135135
expectType<IsStringLiteral<Tagged<Uppercase<string>, 'Tag'>>>(false);
136136
expectType<IsStringLiteral<Tagged<number, 'Tag'>>>(false);
137137
expectType<IsStringLiteral<Tagged<'foo' | 'bar', 'Tag'>>>(true);
138-
expectType<IsStringLiteral<Tagged<'foo' | 'bar' | `on${string}`, 'Tag'>>>({} as boolean);
139-
expectType<IsStringLiteral<Tagged<'1st' | '2nd' | '3rd' | number, 'Tag'>>>({} as boolean);
138+
expectType<IsStringLiteral<Tagged<'foo' | 'bar' | `on${string}`, 'Tag'>>>(boolean);
139+
expectType<IsStringLiteral<Tagged<'1st' | '2nd' | '3rd' | number, 'Tag'>>>(boolean);
140140

141141
expectType<IsStringLiteral<Tagged<string, 'Tag'> | Tagged<number, 'Tag'>>>(false);
142142
expectType<IsStringLiteral<Tagged<'foo', 'Tag'> | Tagged<'bar', 'Tag'>>>(true);
143-
expectType<IsStringLiteral<Tagged<'foo' | 'bar', 'Tag'> | Tagged<number, 'Tag'>>>({} as boolean);
144-
expectType<IsStringLiteral<Tagged<'foo' | 'bar', 'Tag'> | number>>({} as boolean);
143+
expectType<IsStringLiteral<Tagged<'foo' | 'bar', 'Tag'> | Tagged<number, 'Tag'>>>(boolean);
144+
expectType<IsStringLiteral<Tagged<'foo' | 'bar', 'Tag'> | number>>(boolean);
145145

146146
// Uncollapsed unions (e.g., `'foo' | 'bar' | (string & {})`)
147147
expectType<IsStringLiteral<'foo' | 'bar' | (string & {})>>(false);
148148
expectType<IsStringLiteral<LiteralUnion<'foo' | 'bar', string>>>(false);
149149
expectType<IsStringLiteral<LiteralUnion<'onClick' | 'onMouseDown', `on${string}`>>>(false);
150-
expectType<IsStringLiteral<LiteralUnion<'press' | 'onClick' | 'onMouseDown', `on${string}`>>>({} as boolean);
151-
expectType<IsStringLiteral<LiteralUnion<'foo' | 'bar', number>>>({} as boolean);
150+
expectType<IsStringLiteral<LiteralUnion<'press' | 'onClick' | 'onMouseDown', `on${string}`>>>(boolean);
151+
expectType<IsStringLiteral<LiteralUnion<'foo' | 'bar', number>>>(boolean);
152152
expectType<IsStringLiteral<Tagged<LiteralUnion<'foo' | 'bar', string>, 'Tag'>>>(false);
153-
expectType<IsStringLiteral<Tagged<LiteralUnion<'click' | 'onMouseDown', `on${string}`>, 'Tag'>>>({} as boolean);
153+
expectType<IsStringLiteral<Tagged<LiteralUnion<'click' | 'onMouseDown', `on${string}`>, 'Tag'>>>(boolean);

test-d/is-primitive.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import type {
1010
} from '../index.d.ts';
1111
import type {Numeric} from '../source/numeric.js';
1212

13-
type stringLiteral = '';
13+
type stringLiteral = 'aA';
1414
type numberLiteral = 1;
1515
type bigintLiteral = 1n;
1616
type booleanLiteral = true;
1717
type symbolLiteral = typeof tag;
1818
type numericLiteral = numberLiteral | bigintLiteral;
1919

20+
declare const boolean: boolean;
21+
2022
// Primitives should be true
2123
expectType<IsPrimitive<string>>(true);
2224
expectType<IsPrimitive<number>>(true);
@@ -73,9 +75,9 @@ expectType<IsStringPrimitive<Lowercase<'xyz'> | Capitalize<'abc'>>>(false);
7375
// Union of literals and non-literals return `true`
7476
expectType<IsStringPrimitive<Uppercase<string> | (string & {})>>(true);
7577
expectType<IsStringPrimitive<Lowercase<string> | (string & {})>>(true);
76-
expectType<IsNumericPrimitive<(number & {}) | 1 | 2 | 3>>(true);
7778

7879
// Boundary types
80+
expectType<IsStringPrimitive<{}>>(false);
7981
expectType<IsStringPrimitive<any>>(false);
8082
expectType<IsStringPrimitive<never>>(false);
8183

0 commit comments

Comments
 (0)