-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Alias type for indexing a generic object with transform breaking change in 4.2.2+ #44054
Comments
A shorter repro would be appreciated; I'm sure this can be cut down quite a bit. |
@RyanCavanaugh okay shortened it |
This example in 4.1 just returns type ValOf<T> = T[keyof T];
const base = {
color: "green",
} as const;
function getFromPropsNotShared<MoreProps extends {}>(
combined: PropsNotShared<typeof base, MoreProps>,
prop: keyof PropsNotShared<typeof base, MoreProps>
): /**
* Produces error in typescript 4.2.x but succeeds in 4.1.5
*
* Succeeds if you replace the ValOf<...> type annotation with
*
* PropsNotShared<typeof base, MoreProps>[
* keyof PropsNotShared<typeof base, MoreProps>
* ]
*
* (which seems like it means the same)
*/
ValOf<PropsNotShared<typeof base, MoreProps>> {
return combined[prop];
}
type PropsNotShared<LHS extends {}, RHS extends {}> = Omit<RHS, keyof LHS> &
Omit<LHS, keyof RHS>;
const p = getFromPropsNotShared({length: 0, color: "green"}, "length");
p.asdf; // no error |
@RyanCavanaugh Hmm I think that's just because it wasn't inferring the generic type parameter exactly, if you change it to do so it does error there: type ValOf<T> = T[keyof T];
const base = {
color: "green",
} as const;
function getFromPropsNotShared<MoreProps extends {}>(
combined: PropsNotShared<typeof base, MoreProps>,
prop: keyof PropsNotShared<typeof base, MoreProps>
): /**
* Produces error in typescript 4.2.x but succeeds in 4.1.5
*
* Succeeds if you replace the ValOf<...> type annotation with
*
* PropsNotShared<typeof base, MoreProps>[ keyof PropsNotShared<typeof base, MoreProps> ]
*
* (which seems like it should mean the same)
*/
ValOf<PropsNotShared<typeof base, MoreProps>> {
return combined[prop];
}
type PropsNotShared<LHS extends {}, RHS extends {}> = Omit<RHS, keyof LHS> &
Omit<LHS, keyof RHS>;
const p = getFromPropsNotShared /* precise generic type param */ <{length: 0}>({length: 0, color: "green"}, "length");
p.asdf; // now error Furthermore, given that: type ValOf<T> = T[keyof T]; ...the part that seems most strange to me is where in 4.2 (but not in 4.1), an error will be produced when ValOf< PropsNotShared<typeof base, MoreProps> > but not when annotated with return (in either 4.2 or 4.1): PropsNotShared<typeof base, MoreProps>[ keyof PropsNotShared<typeof base, MoreProps> ] Which is weird, right? Because the only difference is Replacing Between both 4.1 and 4.2 there are correctly errors (i.e. not any) in your example if explicit type params are given: const p = getFromPropsNotShared /* precise generic type param */ <{length: 0}>({length: 0, color: "green"}, "length");
p.asdf; // now error |
@iPherian can you reduce this to a minimal repro of what you think the bug is and log a new issue? Thanks! |
@RyanCavanaugh okay opened a new issue #44095 |
Bug Report
π Search Terms
index generic with alias, alias generic
π Version & Regression Information
β― Playground Link
Playground link
π» Code
π Actual behavior
Says the value returned by getFromPropsNotShared() is not assignable to the annotated return type. Works if you replace it with a similar annotation without using the
ValOf
generic.ValOf
is awfully convenient though especially for longer type expressions.π Expected behavior
Would expect it to succeed, because the
ValOf
alias that fails seems like it means the same as what works ( manually write outT[keyof T]
) but cleaner to write. Also because it worked in 4.1.5.The text was updated successfully, but these errors were encountered: