-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Any reason why DANGEROUS operations like this work and surprisingly does not work in ONLY one edge case? #55763
Comments
Duplicate of #13347. Readonly objects are implicitly compatible with the mutable versions. |
@MartinJohns I have highlighted many other points besides that just one point. It is not a full duplicate of the #13347, only one point matches with the issues I have highlighted |
Also, fyi, this is not a crash. "Crash" refers to when the compiler crashes. |
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@abaldawa The reason why read-only arrays are not assignable to mutable arrays is explained by #13347 (comment):
(Note that |
π Search Terms
"generics", "generics type constraints", "generics record index key value change"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true#code/PTAEEYDpQZQYwKYDsCGAnAlge1AWlAIKgAiKSA5gmlgK4DOoWADlSgC7ZKgDuAFhnF6gAKjFAoANhKzc6AKABmNJHA5YuAWxpt2CcAAosAIwBWALlAAlBHCxoAJgB46bTBQA0oZQGskMpAB8AJSgAN5yoJGMppDeCACeoAC8EADcoCCgbPwMGAz2ZJTU9NAAcjhU1GhyAL5ycrZILtEm4MlhcfEWAOR0WBoIoJ3dNan1Wjpseoam4EHpmTC8tBL2ImKVdozaoLxUgyi5bKCbaAy0x3zIoEyHdBgU4qBoCCj26hKJbDh+XC9vH3i9UafQkCEg0nIM1asQSkG+ABksHBJAgkdwqABhQ4IfRBeYZMAAUQAGpiiQAFYQASQA8qULJ1QHlQH5uKyaBojFRQKVacJQC43ORoHJMrgJZKpdKZdLQETSsQ8LLcPUxWAAEzQeDIdDYPCgAAS+3WNxeADdkGwGKdQApqBpdigmCwkA9yKAjDt3khusdfDIeLwgUoVGpNNpdBrHAQ0OQbQAPKZIewMay2BzOVzuzw+NmBALQiyx+MhcJRQmgADqwdA7wQDGyLNtFwA-BEosYTLDEilwAswMIcic0FVztptTg+cJDdTSgBxUAorjc0ATXRrPYveoVjAKfTdYbMrhd0AAMjPWXiLCwChaPeSSRS3SQnO5aG6ZY7FcimT5LSGBIGGXeUADUiUsT1BkEQoEDWPxjg0dgpjQLI9ivFhyBodB7GgGt4lbQj2x-TsYiZFINQHUBqWOFwMCkEcql6bZjgeOgmAwKZGDvSQJDQwY2GvBAsJw79QDqOoGnUZouw1dpQk6Ho+gGQD4hGMY5HXKYNWhDV5mBaSsDBCEsChWSe3hLAkRRMF0SxHE8QJTImSrakEQRKDBWzR4t3BUAKQggAxIlMWEdVlRVSLZXlRUIplNUxQAKkSiJEtAABmUVQDS1LQECozpG4d1YEQVBMBwLURGHOgaDQJhMHuChPhIWkiTEaczQQS0kGOW17X6J0XWQd1cp9P0hjZIMgWy4BFGUVRODXSMpnSmM40TZNUysGw7CcIUcy8JAA24QIAG0AF1Cy7Yt1q-Xd9y7U6AAZzrukiWme86Hz7KihxZYhWva-lGLscc2Dw4N4gIsSJIMppji7dL2lOhSEiU-pBmGGpzo0rSEHS6F0v0qSmiM8FIUJz7WwsxFkVROy0GxOhcXxKjSXJKk6QZVTmQYSbXy5HkOv2ihRXFKKJalGKlUi+pMgAVm1Uq9RwfAmwYbg7G8BhDhOBMWFUOCSeaf4fU+WM0AsU3AS84ULuR7oFAwM42G6HG4eaLT3Qtiw838e2UmtpBzdHX7h1OMGlzsF5VE+cLIuluLVXCgAWJXdXKg0QNee5mpgihBiD5quxsf0gON44i-iWlTHk7wLA1GpxGA6S2A0kFEOW90a-MbaMz27zyFzI78wCdoq57sYvYoHvYnaXpB+6KjyhB1DfObngECkeoJ7IyzAowBM4P0PSw5ZVz3JEQ1LFpKsN8PbpedZHBpAL1CUA5QXULZeOJcTyWgA
π» Code
π Actual behavior
1] If a function accepts an argument of type
Record<string, unknown>
then the function can change the value of any key to ANY value. While this makes sense within the function, but, if you pass a well structured object to this method as an argument then the function can change any key to any value and that will result in runtime error and there is no compile time error when a well structured object is passed to a function acceptingRecord<string, unknown>
. But, if you pass a readonly array to non readonly array then it errors out properly.2] If a function accepts the same argument type
Record<string, unknown>
via generics then changing ANY keys of the object within the function is not possible.3] If the function accepts the argument of type
Record<string, unknown>[]
this time as a array then the function can change any key of any object within the array. This is in stark contrast topoint 2
where it is not possible.4] Readonly array cannot be assigned to non readonly array but readonly object CAN be assigned to non readonly object. Why this difference in functionality?
π Expected behavior
1] Readonly object should NOT be assignable to
Record<string, unknown>
as any keys can be mutated within them causing dangerous side effects.2] The behaviour of
Record<string, unknown>
should be the same regardless of whether it is enforced via generics or directly as a function argument type.3] Generics constraint
Record<string, unknown>[]
i.e. array variant (here any key of any object within the array can be changed) does not seem to behave like generic constraint enforced viaRecord<string, unknown>
i.e. non array variant (here no object keys can be changed)Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: