-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Object spread drops index signature #27273
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
Comments
I think it's extremely arguable one way or the other. Certainly if you introduce a non-conforming property, the original intent of the index signature is lost. |
Object.assign is able to handle the types correctly, why can't Object Spread?
|
As mentioned above, the type inference of Here's an newer playground link that shows that this behaviour still exists in TS 4.5: |
I was linked here from a StackOverflow question I asked, after noticing during code review a variable being assigned an incorrect type in a way that the compiler did not catch. My minimal reproduction: const br: Record<string, number> = {b: 0};
const abr: Record<string, string> = {a: "", ...br};
console.log(abr); Here |
Why is this still open? It's a clear type violation and there's no reason for it. |
@RyanCavanaugh How is this arguable both ways? It's objectively a type violation, what possible argument could there be to not fix it? It's been 5 years. |
@coolCucumber-cat maybe it wasn't your intent, but the above messages don't give off a great vibe IMO - while it can be frustrating to run into problems, this is an open source project that you use for free, and this issue makes it clear that the TS team have discussed this on multiple occasions over the years. It's still open for a reason, and it isn't going to be solved any quicker but saying stuff like "it's been 5 years", etc. |
@joealden Yes but it's a really common issue, it's a clear violation, it shouldn't be hard to fix, it has Microsoft behind it, it's open source and it's been 5 years. What could you possibly have against fixing this? Or does fixing this require knowing what the dog is doing, what Victoria's secret is and what Obama's last name is? |
@coolCucumber-cat feel free to create a PR for this if you think it'd be straight forward - as you can see in this thread I would like this resolved too, the difference is that I'm not demanding it |
@joealden Straight forward for someone who has a basic understanding of the codebase and how it works. If someone that knows a place really well gets us lost there, doesn't mean I can fix it just because I know we're lost. |
@joealden didn't you say it was open for a reason and they're still discussing it? So my PR will just be rejected. You're implying the only reason it hasn't been fixed is because everyone is too lazy. So why don't you fix it then? Or some of the developers? |
@coolCucumber-cat Please tone it down or you're going to get a warning from the maintainers. Unlike #56431, there is no type violation demonstrated in this issue - all the types are inferred, and none of the inferred types are wrong, just inconvenient because the properties described by the index signature become inaccessible. |
I think my example illustrates a type unsoundness here, not merely an inconvenience? I end up with a record that says it only has string values, but in reality it has a number value. |
@bmillwood I was talking specifically about the example in the OP (the original bug report). Your example is covered by #56431, and I agree it is incorrect. |
I don't think I agree that the current behavior would be the the most desired or intuitive behavior. However, regardless of which way you feel, shouldn't the behavior at least be consistent between |
TypeScript Version: 3.0.1
Search Terms:
spread index signature, spread indexer
Code
Expected behavior:
Index signature exists on all object types.
Actual behavior:
Index signature is lost if object literal contains another property assignment. I'm not entirely sure what the type of
v6
should be.Playground Link: https://agentcooper.github.io/typescript-play/#code/CYUwxgNghgTiAEA3WSBc8BK4D2NgB4BnAFxgEsA7AcwBp4TzqA+AbgCg2IRikAmeALzwA3gDpxiAL4t4AelnwA7rgDWhTtyQBmQSKjoA5AbrjRUmfPiVQAD3pkqFKMQCuceBGwkNPRABZdMQk6fXgjaTkFQigAWwQoQngoACNsRBAfJABWQNCARhMJCMtrEDtCBydXd08SOkIAC2wXCGB4ZIQDYVCKFxiOmDoAbRt0BkoqAF10Xv6QGHgAH3pSCckDTMQANkChgDk+gYAKAEpp+AL4U3NI+BAYgAdiAE94bGSAK3AeF4eEACeaABl+pNFptDphYQjMarajnWYDJYrRhUdZAA
Related Issues:
#16694
#16373
The text was updated successfully, but these errors were encountered: