-
Notifications
You must be signed in to change notification settings - Fork 577
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
chore: update generated array type definitions in TypeScript to be readonly #12947
Conversation
{proxyContextValue.proxies && | ||
[...proxyContextValue.proxies] |
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 that proxies
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> { |
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. Since any
is a top type, everything in TypeScript extends any
, so the type constraint provides zero protection
At some point, we'd probably want to turn this into
export interface Foo<R = unknown> {
// Stuff
}
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
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