fix(prop): preserve generic types when used with indexBy #1204
+41
−4
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.
Problem
Using
propwith generic constrained types would lose type information when passed to functions likeindexBy, making it impossible to index the resulting record. This was a regression introduced in v2.27.0.Root Cause
The single-key data-last overload of
propwas returningNoInfer<Prop<T, Key>>:When used with generic types,
Prop<T, "id">(even though structurally equivalent toT["id"]) was not recognized by TypeScript as compatible withT["id"]for Record indexing purposes. This is because theProphelper uses distributive conditional types which prevent TypeScript from simplifying the type in generic contexts.Solution
Removed the problematic single-key data-last overload and rely on the existing fallback overload that returns
T[K]directly:This fallback overload:
T[K]directly (compatible with Record indexing)Impact
The fix is minimal and surgical - only one overload was removed. All existing tests pass, and the new test case verifies the fix works correctly:
Note: The return type is
T | undefined(notT) becauseindexByreturns aBoundedPartialrecord, which correctly models that not all keys may be present.Testing
Fixes #[issue number]
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.