Skip to content

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

Closed
@yamachu

Description

@yamachu

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

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions