Skip to content

Returing a generic argument from a function allows returning a wrong type #59327

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

Closed
StyleShit opened this issue Jul 17, 2024 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@StyleShit
Copy link

🔎 Search Terms

  • Wrong generic return type
  • Function generic return type mismatch
  • Returning a generic allows returning a wrong type

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about returning a generic type

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.3#code/GYVwdgxgLglg9mABKSAeAKogpgDylsAEwGdEBvRABwCc5KAuRARkQF8A+ACgENqBzRugCUg8gChEialighqSMhMmIAdGt58ANEsk06jAORg4UALRMD2yawDcY1mLEQExKIgBu3ADYgsiALzI4BCcFHoMzGxCdsqIAPRxiAB6APxiQA

💻 Code

function func<T extends { prop: 1 }>(arg: T): T {
  return {
    ...arg,
    prop: 'not-1',
  };
}

const value = func({ prop: 1 });
    // ^? { prop: 1 }

🙁 Actual behavior

The function allows returning a wrong type, which results in an error in runtime

🙂 Expected behavior

A compile error in the return statement

Additional information about the issue

It actually started while me & @IdoBouskila were researching a similar bug (playground):

type Fruit =
  | {
    name: string;
    count: 1 | 2 | 3;
    available: true;
  }
  | {
    name: string;
    count: 0;
  }

function func<T extends Fruit>(fruit: T): T {
  return {
    ...fruit,
    count: fruit.count + 1,
  };
}

const value = func({ name: 'apple', count: 0 });
    // ^? { name: 'apple', count: 0 }

In addition, when removing the return type, the inferred return type is still unexpected (playground):

function func<T extends { prop: 1 }>(arg: T) {
  return {
    ...arg,
    prop: 2,
  };
}

const value = func({ prop: 1 });

const prop = value.prop;
    // ^? 1

I would've expected it to be number.

Am I missing something here?

@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 17, 2024

Duplicate of #50559 / #50185 / #10727. Used search terms: generic spread return in:title

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants