- Notifications
You must be signed in to change notification settings - Fork928
chore: update generated array type definitions in TypeScript to be readonly#12947
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
{proxyContextValue.proxies && | ||
[...proxyContextValue.proxies] |
ParkreinerApr 12, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is the only change – check thatproxies
is defined, and then if it is, spread it into a new array, and mutate that instead of the original
All the other code is exactly the same
} | ||
// From codersdk/genericslice.go | ||
export interface Foo<R extends any> { |
ParkreinerApr 15, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Not for the future:R extends any
actually doesn't do anything at all. Sinceany
is a top type, everything in TypeScript extendsany
, so the type constraint provides zero protection
At some point, we'd probably want to turn this into
exportinterfaceFoo<R=unknown>{// Stuff}
Parkreiner left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Double-checked everything, as well as the story changes, and nothing seems to have gone awry.
My main worry at first was that removing some of the accidental mutations would break things, but we just happened to luck into the mutations causing really small, imperceivable changes. Definitely no-nos as far as React best practices, but we cut them off before they could do anything really nasty
Uh oh!
There was an error while loading.Please reload this page.
No issue to link – this was a pain point when trying to bring the Coder generated types into Backstage as an SDK (similar to how the VS Code extension is doing things).
Changes
Notes
Before, we had types like this:
With this, type
Value
'sworkspaces
property can't be reassigned, but the underlying array is still fully mutable. TypeScript is perfectly happy with this:With the update, the type definitions now look like this:
Both the property on the object, and the array the property points to are readonly
Especially since both React and React Query are built around the assumption that 99% of their data is fully immutable, accidental mutations have a risk of introducing bugs that would be incredibly hard to track down