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

3.9 Regression Merge Types #38571

Closed
micheleangioni opened this issue May 14, 2020 · 5 comments
Closed

3.9 Regression Merge Types #38571

micheleangioni opened this issue May 14, 2020 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@micheleangioni
Copy link

micheleangioni commented May 14, 2020

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

@x87
Copy link

x87 commented May 14, 2020

This is expected due to a breaking change #37195

@micheleangioni
Copy link
Author

Are you sure @x87 ? The NON optional _id property is causing the problem.

@x87
Copy link

x87 commented May 14, 2020

@micheleangioni the error says:

Types of property 'otherKey' are incompatible.
      Type 'number | undefined' is not assignable to type 'string | undefined'.
        Type 'number' is not assignable to type 'string | undefined'.(2322)

if you remove the _id property from types WithNumberId and WithNumberString altogether the error remains.
if you remove the otherKey property it's gone.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 14, 2020
@RyanCavanaugh
Copy link
Member

This is the correct behavior. At its initialization, convertedData has a declared property of type string inhabited (possibly) by a value of type number.

@micheleangioni
Copy link
Author

Ok, thanks @RyanCavanaugh for pointing out is was due to 'otherKey' :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants