Open iddar opened 1 week ago
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 :
cx("a", "b")
), e.g.:Also, your "Before" example shows:
cx("foo", null, "bar") // type: never
I tried it and I'm getting:
cx("foo", null, "bar") // type: string
Which is the expected behavior.
Again, I'm not against the idea, I'm just on the fence. So let me know what you think!
Cheers!
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.