types: improve type inference for cx concatenation#64
Open
iddar wants to merge 1 commit intoalexnault:mainfrom
Open
types: improve type inference for cx concatenation#64iddar wants to merge 1 commit intoalexnault:mainfrom
iddar wants to merge 1 commit intoalexnault:mainfrom
Conversation
• Return exact string literal types instead of plain string
• Handle conditional types and unions correctly
• No runtime changes or performance impact
Before:
cx("foo", "bar") → type: string
cx("foo", null, "bar") → type: never
After:
cx("foo", "bar") → type: "foo bar"
cx("foo", null, "bar") → type: "foo bar"
Owner
|
Hey @iddar, thanks for the PR! I really appreciate it. While I think this is an interesting feature, I'm debating adding it for two reasons :
Also, your "Before" example shows: I tried it and I'm getting: Which is the expected behavior. Again, I'm not against the idea, I'm just on the fence. Cheers! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Improved Type Inference for cx Function
This PR enhances the type system for the
cxfunction to provide more accurate type literals in the return type without affecting runtime performance.Changes
FilterStringstype helper to process tuple typesJoinStringstype helper to generate exact string literal typesType System Improvements
Before
After
Implementation
The solution uses TypeScript's type system to:
Justification
Notes
cx(...["foo", "bar"]) Considered but not included
While analyzing type improvements, we found an edge case with spread arrays:
when the array is statically known the workaround is to use the
as constassertion:Same situation for object properties:
We decided not to cover this case because:
Type System Complexity: Adding support for spread arrays would significantly increase type system complexity.
Common Usage: This pattern is rarely used in practice, as most calls to
cxuse direct string literals or variables.Please let me know if you'd like me to explain any part of the implementation in more detail.