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

Spreading multiple objects into one doesn't type check correctly #51433

Closed
mhanuszh opened this issue Nov 7, 2022 · 3 comments
Closed

Spreading multiple objects into one doesn't type check correctly #51433

mhanuszh opened this issue Nov 7, 2022 · 3 comments

Comments

@mhanuszh
Copy link

mhanuszh commented Nov 7, 2022

Bug Report

πŸ”Ž Search Terms

spread operator
object types

πŸ•— Version & Regression Information

Version 4.8.4
Version 5.0.0-dev.20221103

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function processStrings(input: {[index: string]: string}) {
	// Expecting an object with values of string type (with `length` property)
	for (var key in Object.keys(input)) console.log(input[key].length);
}

function test(parameters: {[index: string]: string | number}) {
	var myStuff = {...{asd: "qwe"}, ...parameters};
	// No errors here, while `myStuff` clearly can contain numbers
	processStrings(myStuff)


	var myStuff2 = {...parameters};
	// When leaving out the other object, the error is now correctly determined
	processStrings(myStuff2)
}

// Providing an object with value of number type
test({"key": 123});

πŸ™ Actual behavior

The myStuff variable doesn't receive the predictable {[index: string]: string | number} type, therefore the script crashes at runtime

πŸ™‚ Expected behavior

The myStuff variable should receive the predictable {[index: string]: string | number} type, therefore the IDE or the compiler should show errors about this

@fatcerberus
Copy link

fatcerberus commented Nov 7, 2022

See #50559 and, in particular, #50185 (comment).

Spread is currently modeled using intersection types, which sometimes produces the wrong type. To model this correctly TS would need a dedicated type operator for spread.

@mhanuszh
Copy link
Author

mhanuszh commented Nov 7, 2022

See #50559 and, in particular, #50185 (comment).

Spread is currently modeled using intersection types, which sometimes produces the wrong type. To model this correctly TS would need a dedicated type operator for spread.

So, this is a known TS limitation. Thank you for the clarification!

@mhanuszh mhanuszh closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2022
@fatcerberus
Copy link

fatcerberus commented Nov 7, 2022

Yeah, specifically the limitation is that TS models { ...t, ...u } as T & U, but this is wrong when t and u have overlapping properties (a string index signature is considered to overlap with everything for this purpose).

The issue you want to follow is #10727.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants