-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Use generics for Select arrays #7036
Conversation
Generate changelog in
|
21585a8
to
af3c19a
Compare
@@ -40,13 +40,13 @@ const INTENTS = [Intent.NONE, Intent.PRIMARY, Intent.SUCCESS, Intent.DANGER, Int | |||
|
|||
export interface MultiSelectExampleState { | |||
allowCreate: boolean; | |||
createdItems: Film[]; | |||
createdItems: readonly Film[]; |
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.
The modification of the examples here in this PR isn't strictly necessary, though it helps to serve as a practical example of how to migrate existing Select/MultiSelect components to use readonly arrays. It's also worth noting that the examples here still compile fine if they are not switched to use readonly arrays, demonstrating the backwards compatibility of these type changes.
All the docs/test changes are contained within: 19cdbee
Update examples and tests to use readonly arrayBuild artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job. |
af3c19a
to
19cdbee
Compare
@@ -124,7 +124,7 @@ export class MultiSelectExample extends React.PureComponent<ExampleProps, MultiS | |||
|
|||
return ( | |||
<Example options={this.renderOptions()} {...this.props}> | |||
<MultiSelect<Film> |
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.
The omission of the specified Film
generic parameter to the component is necessary here, otherwise we'll get a type error:
'readonly Film[]' is 'readonly' and cannot be assigned to the mutable type 'Film[]'
Another way to solve this is to explicitly define the second type parameter, e.g.
<MultiSelect<Film, readonly Film[]>
Update examples and tests to use readonly arrayBuild artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job. |
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.
Nice!
nextItems = results.items; | ||
nextCreatedItems = results.createdItems; | ||
nextItems = results.items.slice(); | ||
nextCreatedItems = results.createdItems.slice(); |
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.
Do we have to slice these? We're not actually mutating either of them below.
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.
Ah, yeah, good call. I originally added the slice here since, otherwise, we'd be assigning the readonly arrays from results (results.items
and results.createdItems
) to mutable variables, which results in a type error. We can remove the slice altogether and make everything consistently readonly. 5b8c385
Invalidated by push of 5b8c385
b72900e
to
25f1aa5
Compare
Fix typo in Suggest testsBuild artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job. |
Fixes #0000
Checklist
Changes proposed in this pull request:
This change is an alternative approach to #6999, allowing arrays passed to Select (as well as other components that take arrays) to be marked as readonly. It also preserves backwards compatibility with previous types such that the new prop types will not break existing consumers of these components where they are already passing mutable arrays. This will make it easier to transition existing implementations to use readonly arrays.
Reviewers should focus on:
Screenshot