-
Notifications
You must be signed in to change notification settings - Fork 4.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
data: Begin adding types, starting with redus-store/meta/actions #32855
data: Begin adding types, starting with redus-store/meta/actions #32855
Conversation
Size Change: 0 B Total Size: 1.04 MB ℹ️ View Unchanged
|
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 looks like a good start. I mentioned some things to consider as we work on types for the data package.
The tsconfig
is missing references
, can we make sure that's set correctly before merging this?
* | ||
* @return {Object} Action object. | ||
* @return {{ type: 'START_RESOLUTION', selectorName: string, args: unknown[] }} Action object. |
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.
TypeScript features are really nice with action creators. Rather than effectively writing out the object twice, we use a const assertion and let type inference do the work. No need annotate the return type.
export function someAction( arg ) {
return {
type: 'SOME_ACTION',
payload: arg,
} as const; // <- "const assertion"
}
One thing to consider in the entire data layer is the use of readonly types. We could use mapped types like TypeScript Readonly<Type>
utility (shallowly sets readonly
), some DeepReadonly
implementation, or by adding readonly
manually:
* @return {{ type: 'START_RESOLUTION', selectorName: string, args: unknown[] }} Action object. | |
* @return {{ readonly type: 'START_RESOLUTION', readonly selectorName: string, readonly args: readonly unknown[] }} Action object. |
Considerations:
- The types are more verbose
- It may be applied inconsistently. There's room for error.
- Doing it in the action creators themselves would require users to follow the same pattern.
- This better reflects usage and expectations.
- We may be able to apply readonly at the framework level in store creation/registration, benefitting more users of the library. This won't provide string literal
action.type
by default like const assertions can, but I don't think there's any way around that.
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.
So... which do we do? Convert the file to TS and use the const assertion or make the types readonly?
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 a larger consideration than this PR, it's something we should keep in mind especially as we introduce types around Store
objects.
My preference is to use as const
for action creators. It produces the best result with the lowest effort. A possible downside is that the documentation generator may not handle it well, I'm not sure and haven't tested.
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.
My preference is to use as const for action creators. It produces the best result with the lowest effort.
Should we start with this approach and make changes only if necessary?
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 documentation generator isn't able to infer return types (it doesn't actually run TypeScript compiler, just looks at the static code), they must be explicity declared, so using as const
is hindered in that regard. Here these files are not affected by the documentation generator so we can use as const
without any problem, but we might not be able to do so in all cases 🤷
}, | ||
"include": [ | ||
"src/redux-store/metadata/actions.js" | ||
] |
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 missing references
.
There aren't currently any references being utilized by the types, right? The pattern I've been following is to add to |
References are required to ensure projects are built by TypeScript in the correct order. Ideally, it represents the project faithfully from the start, and we'd all of these as references: gutenberg/packages/data/package.json Lines 31 to 36 in 77bc9ca
It's true it will work because we're only "compiling" a single file right now that doesn't depend on the types, but it seems preferable to get the setup in place and correct rather than having to deal with things as the introduce problems. Project references are error prone in my experience and it's not always obvious to folks where issues come from. If |
972ed5f
to
0ad2638
Compare
0ad2638
to
2049dcf
Compare
Description
Start adding types to
data
. Just adds type checking to a simple, low-level module in the package, nothing too complicated.Part of #18838
How has this been tested?
Type checks pass.
Types of changes
New feature.
Checklist:
*.native.js
files for terms that need renaming or removal).