Skip to content

🐛 Bug: [type-operations/template-literal-type-shenanigans/03-split-on] Incorrect logic in solution #283

New issue

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

Open
4 tasks done
ryo-utsunomiya opened this issue Aug 10, 2024 · 0 comments
Labels
type: bug Something isn't working

Comments

@ryo-utsunomiya
Copy link

ryo-utsunomiya commented Aug 10, 2024

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.
  • This is the appropriate issue form for the bug I would like to report.

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:

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];
@ryo-utsunomiya ryo-utsunomiya added the type: bug Something isn't working label Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant