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

It's incorrect to resolve spread interpolation as intersect #56006

Closed
gomain opened this issue Oct 6, 2023 · 2 comments
Closed

It's incorrect to resolve spread interpolation as intersect #56006

gomain opened this issue Oct 6, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@gomain
Copy link

gomain commented Oct 6, 2023

🔎 Search Terms

object spread generic intersect

🕗 Version & Regression Information

  • This is a crash:
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about: no mention in the FAQ
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20231005#code/GYVwdgxgLglg9mABAWwIYGsCmB1APAeUUwA8pMwATAZ0QG9FUAuBsAT0QF8AaRAIQD4AFHGb4eqAEzNeiAJR0AUIkQAnTFBAqk9AHR6445pJ4cA3ArMKFEBFSiIA7ogC8KDDkH0mdRACNmAIwMNDZgdpzcPhDMEsGIoeEcsubWtnAANpg66XAA5oIOOqg6vi7OrgHJQA

💻 Code

function makeW<O extends { a: any }, B>(o: O, a2: B ) {
  return { ...o, a: a2, };
};

const w = makeW({ a: { b: 1 as const }}, { c: 2 as const });

console.log(w.a.b === 1);

🙁 Actual behavior

Return type of makeW is resolved/inferred as O & { a: B } which is wrong.

the returned value { ...o, a: a2 } overrides any o.a.

🙂 Expected behavior

type of w is Omit<O, "a"> & { a: B }.

Additional information about the issue

Explicit typing works, but should be needed.

function makeW<O extends { a: any }, B>(o: O, a2: B ): Omit<O, "a"> & { a: B } {
  return { ...o, a: a2 };
};

const w = makeW({ a: { b: 1 as const }}, { c: 2 as const });

console.log(w.a.b === 1); // type error, no `b`.
@RyanCavanaugh
Copy link
Member

Duplicate #10727

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 6, 2023
@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 Oct 9, 2023
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

3 participants