Fix TypeScript type narrowing issue with isUrlString

Fixes #212
This commit is contained in:
Sindre Sorhus 2025-09-12 03:58:08 +07:00
parent ef35cc350a
commit c68ad76062
3 changed files with 33 additions and 3 deletions

View file

@ -17,6 +17,7 @@ import is, {
type Primitive,
type TypedArray,
type TypeName,
type UrlString,
} from '../source/index.js';
import {keysOf} from '../source/utilities.js';
@ -757,6 +758,26 @@ test('is.urlString', t => {
});
});
// Type test for urlString narrowing fix (issue #212)
// This test demonstrates that the fix allows proper type narrowing in both branches
(() => {
const value: unknown = 'test';
if (is.urlString(value)) {
// ✅ In true branch: value is narrowed to UrlString
expectTypeOf(value).toEqualTypeOf<UrlString>();
expectTypeOf(value).toMatchTypeOf<string>();
} else {
// ✅ In false branch: value remains unknown (not incorrectly narrowed)
expectTypeOf(value).toEqualTypeOf<unknown>();
// ✅ Manual narrowing to string still works
if (typeof value === 'string') {
expectTypeOf(value).toEqualTypeOf<string>();
}
}
})();
test('is.truthy', t => {
t.true(is.truthy('unicorn'));
t.true(is.truthy('🦄'));