-
Notifications
You must be signed in to change notification settings - Fork 69
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
Add more TypeScript guidelines #7407
Conversation
Test the buildOption 1. Jetpack Beta
Option 2. Jurassic Ninja - available for logged-in A12s🚀 Launch a JN site with this branch 🚀 ℹ️ Install this Tampermonkey script to get more options. Build info:
Note: the build is updated when a new commit is pushed to this PR. |
Hey folks, I mentioned I'd share some more guidelines that have worked well for another project, and here they are! Some of these certainly won't fit this project, so please leave candid feedback. We can clean them up and land whatever works 🙂 |
Size Change: 0 B Total Size: 1.25 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.
Thanks for those great additions @sirreal, I embrace all of them! 🙂
➕ Thanks for sharing these, it's great to collaborate and leverage your previous efforts and expertise.
We can probably put most of them in a
General conventions
file, or something similar, at the top of the contents table.
Yep, this sounds good. We will have more guidelines in the near future, so we can iterate on organising these docs as we go.
We should totally have merged this but it dropped off the radar. I'll resurrect it 🌅 |
@ismaeldcom @reykjalin would you like a final look at this helpful doc before we merge? |
Oh, I also missed this! Looks ready to merge to me. |
## Use const assertions | ||
|
||
Not everything needs a _const assertion,_ but when we want to infer a readonly interface it's a | ||
great option. Here's what that might look like: |
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.
I think we should add a bit more context on what const assertions are (I couldn't make a GH suggestion because the example has markdown code blocks 🙄 )
Use const
assertions
const
assertions are an excellent tool to prevent TS from widening inferred types. This can sound a bit abstract so consider the following code:
function getShapes() {
let result = [
{ kind: "circle", radius: 100 },
{ kind: "square", sideLength: 50 },
];
return result;
}
function useRadius( radius: number ) {
return radius;
}
for ( const shape of getShapes() ) {
if ( shape.kind === "circle" ) {
// TS still thinks shape can be any of the items returned from 'getShapes()' and thus (correctly) infers that 'shape.radius' may be 'undefined'.
useRadius( shape.radius ); // ⛔️ Can't pass 'number | undefined' when the function expects a 'number'.
}
}
const
assertions allow us to get a concrete type without resorting to type guards or type assertion:
function getShapes() {
let result = [
{ kind: "circle", radius: 100 },
{ kind: "square", sideLength: 50 },
] as const;
return result;
}
function useRadius( radius: number ) {
return radius;
}
for ( const shape of getShapes() ) {
if ( shape.kind === "circle" ) {
useRadius( shape.radius ); // ✅ Ok, TS knows that if kind === 'circle' then 'shape' must have a 'radius' prop!
}
}
Not everything needs a const
assertion, but when we want to infer a readonly interface it's a
great option. Here's what that might look like:
[…]
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.
I added this in e43cb6a, would love a review on the language used 🙂
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.
Great addition @reykjalin, definitely helps me understand this benefit of const assertions 💯
Co-authored-by: Eric Jinks <[email protected]> Co-authored-by: Kristófer R <[email protected]> Co-authored-by: Kristófer Reykjalín <[email protected]>
Changes proposed in this Pull Request
This is a follow-up to #7046 that adds more TypeScript guidelines.
These are guidelines that have been helpful for another TypeScript project (Tumblr). They may not all be applicable here. I've added them all in a single file for now for the purposes of discussion. They can be split into separate files when the desired guidelines are established.
Follow-up to #7046 (review)
npm run changelog
to add a changelog file, choosepatch
to leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.Post merge