-
-
Notifications
You must be signed in to change notification settings - Fork 572
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
Split
: Fix return type when passing in non-literal and distribute unions of parameter Delimiter
#1047
base: main
Are you sure you want to change the base?
Conversation
@ghaaj I don't think that's practically very useful, it's much better to simply return Also, what happens in cases like |
This reverts commit 78b7310.
* 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<string>) Co-authored-by: Som Shekhar Mukherjee <[email protected]>
…extends true ? ..` into `SplitHelper` to fix ts2589
Looks like So, we'd probably have to introduce this change as an option. |
type SplitHelper<
S extends string,
_Delimiter extends string,
Accumulator extends string[] = [],
> = S extends `${infer __}`
? IsStringLiteral<_Delimiter> extends true
? _Delimiter extends infer Delimiter extends string
? S extends `${infer Head}${Delimiter}${infer Tail}`
? SplitHelper<Tail, Delimiter, [...Accumulator, Head]>
: Delimiter extends ''
? Accumulator
: [...Accumulator, S]
: never
: string[]
: string[]; This would probably fix the behavior in the case where S is a template literal type, but I'm not sure that resolving |
Yeah, I also think this isn't the correct behaviour, because But, what initially seemed like a bug now seems like a breaking change, so I think it's better to introduce an option (like And, it makes sense to implement this as an option because there may be valid use cases where you'd want |
…be left as they are
…` of function `split`
@ghaaj So, even with However, not sure if we should keep this behaviour consistent with That said, this feels like it's getting overly complicated. I feel we shouldn't change anything when |
Why I restricted For example, how should we resolve I think it's fine as it is now about the behavior when
As for the first one, the change is acceptable because I think the behavior so far is a bug ( |
Umm...I still have few concerns:
While I don’t really have a strong opinion here, I still believe leaving the behaviour unchanged when |
Currently, passing a non-literal string to
Split
's parameterS
orDelimiter
results in unexpected behavior:There is also a problem with
Delimiter
not being distributed correctly when it is a union type:playground
This PR fixes the behavior of
Split
so that it meets the above requirements.As for the first one (
SplitLiteralByNonLiteral
) I adopted the latter, but I don't think it necessarily have to be that way.