镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

3.9 Regression Merge Types #38571

Closed
Closed

Description

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

Activity

  1. x87 commented on May 14, 2020

    @x87

    This is expected due to a breaking change #37195

  2. micheleangioni commented on May 14, 2020

    @micheleangioni
    Author

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

  3. x87 commented on May 14, 2020

    @x87

    Michele Angioni (@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.

  4. RyanCavanaugh commented on May 14, 2020

    @RyanCavanaugh
    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.

  5. micheleangioni commented on May 15, 2020

    @micheleangioni
    Author

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

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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