-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type of mapped or conditional type member incorrectly tracked #51588
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
Comments
It looks like you need #48992 |
Note: |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I don't think this is the same as #48992 because I'm not doing a test to only get keys where the property matches a particular type value: I'm fetching an object member and trying to use a member of that type. The types are hardcoded in this example and the selection is by name, not by the type. TS should be able to figure out that the types match (avoiding the reported error) without my having to specify what those types are. In the first example, I should not have to specify that I'm selecting KeysOfType string (so as to provide some generality), but TypeScript should be able to figure out in that first example that |
Here is another example of what I think is likely a duplicate of this one: type SchoolGroupings = {
Classroom: {
studentNames: string[];
teacherNames: string[];
}
SportsTeam: {
athleteNames: string[];
coachNames: string[];
coordinatorNames: string[];
}
}
declare function maybeGetStrings<SG extends keyof SchoolGroupings>() :
SchoolGroupings[SG][keyof SchoolGroupings[SG]] | undefined | null;
export const demo = function<SG extends keyof SchoolGroupings>() {
const maybeStrings = maybeGetStrings<SG>();
if((typeof maybeStrings !== 'undefined') && (maybeStrings !== null)) {
maybeStrings //has a problematic `& ({} | undefined)` at the end of the type
}
} I would expect the narrowing to leave just If I hard-code the type parameter to |
Bug Report
🔎 Search Terms
2344 mapped conditional keyof member
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code (Covers all three of the examples below)
💻 Code (3 examples)
🙁 Actual behavior
Errors as noted, e.g. in the middle one:
even though any K and F which satisfy the type parameter constraints will lead
RacerDetailsAccessorObj<K>[F]
to extend(...args: any) => any
as required byReturnType<>
. In the Simpler Object Version (shown first) you can see the same effect with a different error code, where the constraint comes from a parameter type to a function instead of another type.Removing
& string
changes the penultimate line of the error stack to include something like this:Overall, it looks here like type F is being interpreted in an overbroad sense, as if it were
keyof <anything>
instead ofkeyof <something much more specific>
. It's ignoring the fact that there are only three valid values for F (which three values those are depends on K). When K is hardcoded rather than a generic type parameter, it works as shown, but hardcoding everything is not a maintainable solution in context.🙂 Expected behavior
All three examples compile without error, even when K is a constrained generic type parameter instead of hardcoded. F can still be interpreted as being limited to one of the valid values for a
keyof
that object type, and TypeScript can see that all possible values match the constraint.The text was updated successfully, but these errors were encountered: