types: improve type inference for cx concatenation #64
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
cx
function to provide more accurate type literals in the return type without affecting runtime performance.Changes
FilterStrings
type helper to process tuple typesJoinStrings
type 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 const
assertion: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
cx
use direct string literals or variables.Please let me know if you'd like me to explain any part of the implementation in more detail.