From 5301915f32b64a2aa3081eacaa844bd91f3a8c0f Mon Sep 17 00:00:00 2001 From: Martin Eneqvist Date: Wed, 22 May 2024 16:41:11 +0200 Subject: [PATCH] Change type guard for whitespace from string to ' ' Closes #206 --- source/index.ts | 9 +++++---- source/types.ts | 2 ++ test/test.ts | 7 +++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/source/index.ts b/source/index.ts index a3a7cf3..63a862a 100644 --- a/source/index.ts +++ b/source/index.ts @@ -10,6 +10,7 @@ import type { Primitive, TypedArray, WeakRef, + Whitespace, } from './types.js'; const typedArrayTypeNames = [ @@ -443,7 +444,7 @@ export function isEmptyString(value: unknown): value is '' { return isString(value) && value.length === 0; } -export function isEmptyStringOrWhitespace(value: unknown): value is string { +export function isEmptyStringOrWhitespace(value: unknown): value is '' | Whitespace { return isEmptyString(value) || isWhitespaceString(value); } @@ -785,7 +786,7 @@ export function isWeakSet(value: unknown): value is WeakSet { return getObjectType(value) === 'WeakSet'; } -export function isWhitespaceString(value: unknown): value is string { +export function isWhitespaceString(value: unknown): value is Whitespace { return isString(value) && /^\s+$/.test(value); } @@ -911,7 +912,7 @@ type Assert = { emptyArray: (value: unknown, message?: string) => asserts value is never[]; nonEmptyArray: (value: T | Item[], message?: string) => asserts value is [Item, ...Item[]]; emptyString: (value: unknown, message?: string) => asserts value is ''; - emptyStringOrWhitespace: (value: unknown, message?: string) => asserts value is string; + emptyStringOrWhitespace: (value: unknown, message?: string) => asserts value is '' | Whitespace; nonEmptyString: (value: unknown, message?: string) => asserts value is string; nonEmptyStringAndNotWhitespace: (value: unknown, message?: string) => asserts value is string; emptyObject: (value: unknown, message?: string) => asserts value is Record; @@ -1301,7 +1302,7 @@ export function assertEmptyString(value: unknown, message?: string): asserts val } } -export function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is string { +export function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is '' | Whitespace { if (!isEmptyStringOrWhitespace(value)) { throw new TypeError(message ?? typeErrorMessage('empty string or whitespace', value)); } diff --git a/source/types.ts b/source/types.ts index e77d73a..258fa00 100644 --- a/source/types.ts +++ b/source/types.ts @@ -75,3 +75,5 @@ export type NodeStream = { export type Predicate = (value: unknown) => boolean; export type NonEmptyString = string & {0: string}; + +export type Whitespace = ' '; diff --git a/test/test.ts b/test/test.ts index 1dd5f2a..8684817 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1793,6 +1793,13 @@ test('is.emptyStringOrWhitespace', t => { t.throws(() => { assert.emptyStringOrWhitespace('unicorn'); }); + + let value = 'test'; // eslint-disable-line prefer-const -- can't use `const` here because then it will be inferred as `never` in the `if` block + if (is.emptyStringOrWhitespace(value)) { + value.charAt(0); // Should be inferred as `'' | Whitespace` and not `never` + } else { + value.charAt(0); // Should be inferred as `string` and not `never` + } }); test('is.nonEmptyString', t => {