Skip to content

3.9 Regression Merge Types #38571

Closed
Closed
@micheleangioni

Description

@micheleangioni

TypeScript Version: 3.9.2

Search Terms: Regression, merge types

Code

type Merge<T, S> = Omit<T, Extract<keyof T, keyof S>> & S;

type WithNumberId = {
  _id: number,
  otherKey?: number,
};

type WithStringId = {
  _id: string,
  otherKey?: string,
};

function convertToStringId<T extends WithNumberId>(data: T): Merge<T, WithStringId> {
  const convertedData: Merge<T, WithStringId> = {
    ...data,
    _id: data._id.toString(),
  };

  if (data.otherKey) {
    convertedData.otherKey = data.otherKey.toString();
  }

  return convertedData;
}

Expected behavior: No error (3.8x is fine)

Actual behavior: Definition of const convertedData gives error

Playground Link: https://www.typescriptlang.org/play/?ssl=20&ssc=35&pln=20&pc=51#code/C4TwDgpgBAshBOBzCAeAKgGigZQHxQF4oB5AWwEth0sBRAD2HgEMBjKgawhAHsAzKTFE49+efADIcAbgBQM0JCgB1SgAsAcgFdSAIwQBJACaEoAbxlQoAfXKGAXFAB22vfAwWo3YKoQBpLgD8Ds66CO4AvrLy4NAq3tiM5I6IRibmljb2UADOicnull4+8P4gQTl5iBFRvJqObOTcjlAsTQBuCMBo3AnwSSmG6FAQDBCOhtnKalqh8Ea4ABSGTMBMDmgAlA5wSKiCcaq9-fNmHq2OuS3tnRCGACIra7AIyNRT8ZUnROmWUAB0AOWqwKv0yDiBTD+mT+wB6lQWGxBkTklnI-CWjz+RT8XA2p1+V0cHXgwFuD1WWO8OJAJghlOKpRhcL6yQRsks4RRUHgEGAmngzXOxNJ90esk5QA

Related Issues: Maybe #38460

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions