-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Complex Union type with generics could not assign return type #43749
Labels
Needs Investigation
This issue needs a team member to investigate its status.
Milestone
Comments
// Without generics: OK
function f1(v: { p: string } | { p: number }): { p: string } | { p: number } {
return { ...v };
}
// Only union: OK
function f2<T extends { p: string } | { p: number }>(v: T): T {
return v
}
// Only spread operator: OK
function f3<T extends { p: string }>(v: T): T {
return { ...v }
}
// Both union and spread operator: NG
function f4<T extends { p: string } | { p: number }>(v: T): T {
// Error: Type '{ p: string; } | { p: number; }' is not assignable to type 'T'.
return { ...v }
} |
RyanCavanaugh
added
the
Needs Investigation
This issue needs a team member to investigate its status.
label
Apr 21, 2021
I found this workaround: function workaround<T>(t: T): asserts t is T { }
// Both union and spread operator
function f4<T extends { p: string } | { p: number }>(v: T): T {
workaround<T>(v);
// No more error
return { ...v }
} |
Looks like this is fixed in nightly build (by #44081 ?) |
Yes! After applying the following change, it works. const test = <
T extends StringableType | NonGivenIdWithTmpId
>(
v: T
): (Omit<T, 'stringableId'>) & { stringableId: StringableIdType } => {
// omit
const { stringableId, ...rest } = v;
return {
...rest,
// bind
stringableId: Number.parseInt(v.stringableId, 10),
}
}; Thank you for noticing! |
Fixed by #44081. Closing. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
I don't know which is the correct behavior...
I would appreciate it if you could change the title of the issue.
🔎 Search Terms
generic return type union extend
maybe related: #43425
🕗 Version & Regression Information
Omit
was not implemented.⏯ Playground Link
Playground link with relevant code
worked 4.3.0-dev.20210315
not worked 4.3.0-dev.20210323
💻 Code
🙁 Actual behavior
cannot assign function argument to return type
🙂 Expected behavior
Union type function arguments given by generics can be used as part of the return type
The text was updated successfully, but these errors were encountered: