From 784facca7c1c8f5da0a2c707f32ac400ed7b42f4 Mon Sep 17 00:00:00 2001 From: ghaaj <91141283+ghaaj@users.noreply.github.com> Date: Thu, 23 Jan 2025 21:26:01 +0900 Subject: [PATCH] `Split`: Just return `string[]` when `S` or `Delimiter` is non-literal * Use `IsStringLiteral` for conditional branches, which is more appropriate than `string extends ..` * Add test cases where `S` or `Delimiter` is not `string` itself but also not a literal (e.g., Uppercase) Co-authored-by: Som Shekhar Mukherjee <49264891+som-sm@users.noreply.github.com> --- source/split.d.ts | 12 +++++------- test-d/split.ts | 14 ++++---------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/source/split.d.ts b/source/split.d.ts index 24e58fcb4..e572b377c 100644 --- a/source/split.d.ts +++ b/source/split.d.ts @@ -1,5 +1,5 @@ -import type {ArrayValues} from './array-values'; -import type {Substrings} from './internal'; +import type {IsStringLiteral} from './is-literal'; + /** Represents an array of strings split using a given character or character set. @@ -24,11 +24,9 @@ array = split(items, ','); export type Split< S extends string, Delimiter extends string, -> = string extends S - ? string[] - : string extends Delimiter - ? [S] | SplitHelper>> - : SplitHelper; +> = IsStringLiteral extends true + ? SplitHelper + : string[]; type SplitHelper< S extends string, diff --git a/test-d/split.ts b/test-d/split.ts index 4941acc50..41491c555 100644 --- a/test-d/split.ts +++ b/test-d/split.ts @@ -45,21 +45,15 @@ expectType< // Split non-literal string by literal string. expectType(split(null! as string, 'foobar')); +expectType(split(null! as Uppercase, 'foobar')); // Split non-literal string by non-literal string. expectType(split(null! as string, null! as string)); +expectType(split(null! as `pre${string}`, null! as `${number}`)); // Split literal string by non-literal string. -expectType< -| ['ABCDEF'] -| ['', ''] -| ['', 'F'] | ['A', ''] -| ['', 'EF'] | ['A', 'F'] | ['AB', ''] -| ['', 'DEF'] | ['A', 'EF'] | ['AB', 'F'] | ['ABC', ''] -| ['', 'CDEF'] | ['A', 'DEF'] | ['AB', 'EF'] | ['ABC', 'F'] | ['ABCD', ''] -| ['', 'BCDEF'] | ['A', 'CDEF'] | ['AB', 'DEF'] | ['ABC', 'EF'] | ['ABCD', 'F'] | ['ABCDE', ''] -| ['A', 'B', 'C', 'D', 'E', 'F'] ->(split('ABCDEF', null! as string)); +expectType(split('ABCDEF', null! as string)); +expectType(split('ABCDEF', null! as Capitalize)); // Recursion depth at which a non-tail recursive implementation starts to fail. const fiftyZeroes = '00000000000000000000000000000000000000000000000000';