Skip to content

Object spread with ternary causes invalid union on property #50922

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
karlhorky opened this issue Sep 23, 2022 · 3 comments
Closed

Object spread with ternary causes invalid union on property #50922

karlhorky opened this issue Sep 23, 2022 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Sep 23, 2022

Bug Report

Originally commented over here: #48326 (comment)

πŸ”Ž Search Terms

spread, ternary, object spread, union, property

πŸ•— Version & Regression Information

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function fn(h: { a: string; b?: never; c?: never } | { a: string; b: string; c: string }) {}

let data = 'a' as 'a' | undefined;

// βœ… This works in TS
fn(data ? { a: '', b: '', c: data } : { a: '' });

// ❌ TS cannot figure this option out, even though it's also valid
fn({
  a: '',
  ...(data ? { b: '', c: data } : {}),
});

// βœ… This works in TS
fn({
  a: '',
  ...(data ? { b: '', c: data } : {} as {[key: string]: never}),
});

πŸ™ Actual behavior

b was recognized to be a string | undefined, although it's only possible to be a string

πŸ™‚ Expected behavior

b should only be a string

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 23, 2022
@RyanCavanaugh
Copy link
Member

We had to change this behavior in 4.2 to avoid an exponential explosion caused by repeated spreads in the same object.

@RyanCavanaugh
Copy link
Member

Thread with more context

@karlhorky
Copy link
Contributor Author

Ok thanks, closing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants