Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
Parkreiner merged 11 commits intomainfromstevenmasley/readonly_gen
Apr 15, 2024

Conversation

Emyrk
Copy link
Member

@EmyrkEmyrk commentedApr 11, 2024
edited by Parkreiner
Loading

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

  • Updates type-generation logic on the Go side to make all TypeScript arrays be readonly (see notes)
  • Updates all UI code in TypeScript to remove any mutable array types, and remove accidental runtime mutations

Notes

Before, we had types like this:

interfaceValue{readonlyworkspaces:Workspace[];}

With this, typeValue'sworkspaces property can't be reassigned, but the underlying array is still fully mutable. TypeScript is perfectly happy with this:

functiondoStuff(value:Value){constnewWorkspace=makeWorkspace();value.workspaces.push(newWorkspace);}

With the update, the type definitions now look like this:

interfaceValue{readonlyworkspaces:readonlyWorkspace[];}

Both the property on the object, and the array the property points to are readonly

functiondoStuff(value:Value){constnewWorkspace=makeWorkspace();// TypeScript compiler complainsvalue.workspaces.push(newWorkspace);// Perfectly fineconstworkspacesCopy=[...value.workspaces];workspacesCopy.push(newWorkspace);}

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

@EmyrkEmyrk marked this pull request as draftApril 11, 2024 21:10
@ParkreinerParkreiner changed the titlechore: types generated handling readonly sliceschore: update generated array type definitions in TypeScript to be readonlyApr 12, 2024
Comment on lines +345 to +346
{proxyContextValue.proxies &&
[...proxyContextValue.proxies]
Copy link
Member

@ParkreinerParkreinerApr 12, 2024
edited
Loading

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

@ParkreinerParkreiner marked this pull request as ready for reviewApril 12, 2024 19:03
}

// From codersdk/genericslice.go
export interface Foo<R extends any> {
Copy link
Member

@ParkreinerParkreinerApr 15, 2024
edited
Loading

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}

Copy link
Member

@ParkreinerParkreiner left a comment
edited
Loading

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

@ParkreinerParkreiner merged commitd9da054 intomainApr 15, 2024
@ParkreinerParkreiner deleted the stevenmasley/readonly_gen branchApril 15, 2024 13:46
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsApr 15, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@ParkreinerParkreinerParkreiner approved these changes

Assignees

@EmyrkEmyrk

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@Emyrk@Parkreiner

[8]ページ先頭

©2009-2025 Movatter.jp