We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
main
SplitOn<"baby", "a"> -> ["b", "by"] SplitOn<"hello my baby", " "> -> ["hello", "my", "baby"]
SplitOn<"baby", "a">
["b", "by"]
SplitOn<"hello my baby", " ">
["hello", "my", "baby"]
The resulting array is in reverse order.
SplitOn<"baby", "a"> -> ["by", "b"] SplitOn<"hello my baby", " "> -> ["baby", "my", "hello"]
["by", "b"]
["baby", "my", "hello"]
projects/type-operations/template-literal-type-shenanigans/03-split-on/solution.ts
The correct implementation of the SplitOn type would be something like the following:
SplitOn
export type SplitOn< Text extends string, On extends string, Results extends string[] = [] > = Text extends `${infer Prefix}${On}${infer Suffix}` ? SplitOn<Suffix, On, [...Results, Prefix]> : [...Results, Text];
Alternatively, it could be written more simply like this:
export type SplitOn< Text extends string, On extends string > = Text extends `${infer Prefix}${On}${infer Suffix}` ? [Prefix, ...SplitOn<Suffix, On>] : [Text];
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Bug Report Checklist
main
branch of the repository.Expected
SplitOn<"baby", "a">
->["b", "by"]
SplitOn<"hello my baby", " ">
->["hello", "my", "baby"]
Actual
The resulting array is in reverse order.
SplitOn<"baby", "a">
->["by", "b"]
SplitOn<"hello my baby", " ">
->["baby", "my", "hello"]
Impacted Project
projects/type-operations/template-literal-type-shenanigans/03-split-on/solution.ts
Additional Info
The correct implementation of the
SplitOn
type would be something like the following:Alternatively, it could be written more simply like this:
The text was updated successfully, but these errors were encountered: