Skip to content
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

Closed
yamachu opened this issue Apr 20, 2021 · 5 comments
Closed

Complex Union type with generics could not assign return type #43749

yamachu opened this issue Apr 20, 2021 · 5 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@yamachu
Copy link

yamachu commented Apr 20, 2021

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

  • This changed between versions 4.3.0-dev.20210315 and 4.3.0-dev.20210323
  • I was unable to test this on prior versions (before v3.5.1) because 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

type GivenIdType = number;
type StringableIdType = number;

interface MyType {
    givenId: GivenIdType;
    stringableId: StringableIdType;
}

type ToPropertyNull<T, K extends keyof T> = { [P in keyof T]: P extends K ? null : T[P] };
type AddTmpIdId<T, K extends keyof T> = ToPropertyNull<T, K> & { tmpId: string };

type StringableType = Omit<MyType, 'stringableId'> & {
    stringableId: string;
}; // { givenId: GivenIdType, stringableId: string }
type NonGivenIdWithTmpId = AddTmpIdId<
    StringableType,
    'givenId'
>; // { givenId: null, stringableId: string, tmpId: string }

const test = <
    T extends StringableType | NonGivenIdWithTmpId
>(
    v: T
): (Omit<T, 'stringableId'>) & { stringableId: StringableIdType } => ({
    ...v,
    stringableId: Number.parseInt(v.stringableId, 10),
});

🙁 Actual behavior

cannot assign function argument to return type

Type '{ stringableId: number; givenId: number; } | { stringableId: number; givenId: null; tmpId: string; }' is not assignable to type 'Omit<T, "stringableId"> & { stringableId: number; }'.

🙂 Expected behavior

Union type function arguments given by generics can be used as part of the return type

@Kiikurage
Copy link

Simpler reproducible code

// 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 RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 21, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.4.0 milestone Apr 21, 2021
@markusjohnsson
Copy link
Contributor

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 }
}

@markusjohnsson
Copy link
Contributor

Looks like this is fixed in nightly build (by #44081 ?)

@ahejlsberg
Copy link
Member

Fixed by #44081. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

5 participants