Skip to content
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

Merged
merged 10 commits into from
Apr 12, 2024
Merged

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Oct 6, 2023

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)


  • Run npm run changelog to add a changelog file, choose patch 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

@botwoo
Copy link
Collaborator

botwoo commented Oct 6, 2023

Test the build

Option 1. Jetpack Beta

  • Install and activate Jetpack Beta.
  • Use this build by searching for PR number 7407 or branch name add-more-typescript-guidelines in your-test.site/wp-admin/admin.php?page=jetpack-beta&plugin=woocommerce-payments

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:

  • Latest commit: 4e38fd1
  • Build time: 2024-04-12 22:57:19 UTC

Note: the build is updated when a new commit is pushed to this PR.

@sirreal
Copy link
Member Author

sirreal commented Oct 6, 2023

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 🙂

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2023

Size Change: 0 B

Total Size: 1.25 MB

ℹ️ View Unchanged
Filename Size
release/woocommerce-payments/assets/css/admin.css 1.08 kB
release/woocommerce-payments/assets/css/admin.rtl.css 1.08 kB
release/woocommerce-payments/assets/css/success.css 158 B
release/woocommerce-payments/assets/css/success.rtl.css 158 B
release/woocommerce-payments/dist/blocks-checkout-rtl.css 2.02 kB
release/woocommerce-payments/dist/blocks-checkout.css 2.02 kB
release/woocommerce-payments/dist/blocks-checkout.js 56.9 kB
release/woocommerce-payments/dist/bnpl-announcement-rtl.css 530 B
release/woocommerce-payments/dist/bnpl-announcement.css 531 B
release/woocommerce-payments/dist/bnpl-announcement.js 19.9 kB
release/woocommerce-payments/dist/cart-block.js 21.5 kB
release/woocommerce-payments/dist/cart.js 4.52 kB
release/woocommerce-payments/dist/checkout-rtl.css 599 B
release/woocommerce-payments/dist/checkout.css 599 B
release/woocommerce-payments/dist/checkout.js 37.8 kB
release/woocommerce-payments/dist/index-rtl.css 40.4 kB
release/woocommerce-payments/dist/index.css 40.4 kB
release/woocommerce-payments/dist/index.js 291 kB
release/woocommerce-payments/dist/multi-currency-analytics.js 1.05 kB
release/woocommerce-payments/dist/multi-currency-rtl.css 3.28 kB
release/woocommerce-payments/dist/multi-currency-switcher-block.js 59.4 kB
release/woocommerce-payments/dist/multi-currency.css 3.29 kB
release/woocommerce-payments/dist/multi-currency.js 54.5 kB
release/woocommerce-payments/dist/order-rtl.css 733 B
release/woocommerce-payments/dist/order.css 735 B
release/woocommerce-payments/dist/order.js 41.7 kB
release/woocommerce-payments/dist/payment-gateways-rtl.css 1.21 kB
release/woocommerce-payments/dist/payment-gateways.css 1.21 kB
release/woocommerce-payments/dist/payment-gateways.js 38.5 kB
release/woocommerce-payments/dist/payment-request-rtl.css 155 B
release/woocommerce-payments/dist/payment-request.css 155 B
release/woocommerce-payments/dist/payment-request.js 12.4 kB
release/woocommerce-payments/dist/product-details-rtl.css 398 B
release/woocommerce-payments/dist/product-details.css 402 B
release/woocommerce-payments/dist/product-details.js 17.2 kB
release/woocommerce-payments/dist/settings-rtl.css 11 kB
release/woocommerce-payments/dist/settings.css 10.8 kB
release/woocommerce-payments/dist/settings.js 201 kB
release/woocommerce-payments/dist/subscription-edit-page.js 669 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal-rtl.css 527 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.css 527 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.js 19.4 kB
release/woocommerce-payments/dist/subscription-product-onboarding-toast.js 710 B
release/woocommerce-payments/dist/subscriptions-empty-state-rtl.css 120 B
release/woocommerce-payments/dist/subscriptions-empty-state.css 120 B
release/woocommerce-payments/dist/subscriptions-empty-state.js 18.5 kB
release/woocommerce-payments/dist/tos-rtl.css 235 B
release/woocommerce-payments/dist/tos.css 236 B
release/woocommerce-payments/dist/tos.js 21 kB
release/woocommerce-payments/dist/woopay-direct-checkout.js 4.67 kB
release/woocommerce-payments/dist/woopay-express-button-rtl.css 155 B
release/woocommerce-payments/dist/woopay-express-button.css 155 B
release/woocommerce-payments/dist/woopay-express-button.js 21.1 kB
release/woocommerce-payments/dist/woopay-rtl.css 4.44 kB
release/woocommerce-payments/dist/woopay.css 4.41 kB
release/woocommerce-payments/dist/woopay.js 70.9 kB
release/woocommerce-payments/includes/subscriptions/assets/css/plugin-page.css 622 B
release/woocommerce-payments/includes/subscriptions/assets/js/plugin-page.js 815 B
release/woocommerce-payments/vendor/automattic/jetpack-assets/build/i18n-loader.js 2.44 kB
release/woocommerce-payments/vendor/automattic/jetpack-assets/src/js/i18n-loader.js 1.01 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-ajax.js 522 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-callables.js 581 B
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/babel.config.js 160 B
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.css 2.37 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.js 13.5 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.rtl.css 2.37 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/about.css 1.03 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-empty-state.css 291 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-order-statuses.css 403 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin.css 3.6 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/checkout.css 299 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/modal.css 742 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/view-subscription.css 572 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/wcs-upgrade.css 411 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin-pointers.js 544 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin.js 9.4 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.js 6.8 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.min.js 3.83 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-coupon.js 544 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-subscription.js 2.52 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.js 22.1 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.min.js 11.6 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/payment-method-restrictions.js 1.29 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/wcs-meta-boxes-order.js 502 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/payment-methods.js 355 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/single-product.js 429 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/view-subscription.js 1.38 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/wcs-cart.js 781 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/modal.js 1.1 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/wcs-upgrade.js 1.27 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.css 392 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.js 3.05 kB

compressed-size-action

docs/typescript/more.md Outdated Show resolved Hide resolved
@ismaeldcom
Copy link
Contributor

Thanks for those great additions @sirreal, I embrace all of them! 🙂

We can probably put most of them in a General conventions file, or something similar, at the top of the contents table. Let's see what @Jinksi thinks too.

docs/typescript/more.md Outdated Show resolved Hide resolved
docs/typescript/more.md Outdated Show resolved Hide resolved
Copy link
Member

@Jinksi Jinksi left a 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.

@Jinksi
Copy link
Member

Jinksi commented Apr 10, 2024

We should totally have merged this but it dropped off the radar. I'll resurrect it 🌅

@Jinksi Jinksi marked this pull request as ready for review April 10, 2024 23:34
@Jinksi Jinksi added pr: ready to merge type: documentation This issue is a request for better documentation. labels Apr 11, 2024
@Jinksi
Copy link
Member

Jinksi commented Apr 11, 2024

@ismaeldcom @reykjalin would you like a final look at this helpful doc before we merge?

@ismaeldcom
Copy link
Contributor

Oh, I also missed this! Looks ready to merge to me.

Comment on lines 164 to 167
## 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:
Copy link
Contributor

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:

[…]

Copy link
Contributor

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 🙂

@Jinksi Jinksi self-requested a review April 11, 2024 23:55
Copy link
Member

@Jinksi Jinksi left a 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 💯

@reykjalin reykjalin enabled auto-merge April 12, 2024 22:54
@reykjalin reykjalin added this pull request to the merge queue Apr 12, 2024
Merged via the queue into develop with commit ff970e3 Apr 12, 2024
22 checks passed
@reykjalin reykjalin deleted the add-more-typescript-guidelines branch April 12, 2024 23:07
naman03malhotra pushed a commit that referenced this pull request Apr 15, 2024
Co-authored-by: Eric Jinks <[email protected]>
Co-authored-by: Kristófer R <[email protected]>
Co-authored-by: Kristófer Reykjalín <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: ready to merge type: documentation This issue is a request for better documentation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants