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

chore: manage typescript at workspace root #36

Merged
merged 23 commits into from
Jun 22, 2023

Conversation

bhongy
Copy link
Contributor

@bhongy bhongy commented Jun 19, 2023

Changes

  • centralize typescript-related devDependencies to project root for version consistency
  • centralize tsconfig to root and have package-specific tsconfig extends it for consistency
  • make typescript config more strict especially around detecting null and undefined
  • fix type errors
  • typecheck test folders
  • add Turbo task typecheck which runs tsc in typechecking mode
  • set up Github Action to run typecheck task

Verification
Checked the produced index.d.ts and index.js files and they're equivalent to the output produced on the main branch.

  • core/dist/index.d.ts exact same output
  • core/dist/index.js equivalent output (seems like munged variable names are swapped but it produces the same result), note "use strict" is removed
  • react/dist/index.d.ts exact same output
  • react/dist/index.js comparable output; expected the changes how we handle null and undefined in context.tsx and useForm.ts.

@changeset-bot
Copy link

changeset-bot bot commented Jun 19, 2023

🦋 Changeset detected

Latest commit: 9ef1967

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@formspree/react Patch
@formspree/core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Jun 19, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
formspree-react-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 22, 2023 3:37am

@@ -22,6 +23,7 @@
"lint-staged": "^13.2.2",
"prettier": "^2.8.8",
"sort-package-json": "^2.4.1",
"turbo": "latest"
"turbo": "latest",
"typescript": "^5.1.3"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: cra-demo still uses 4.7.4; to be upgraded in a follow-up PR

@@ -1,2 +0,0 @@
// @ts-nocheck
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not referenced from anywhere.

@@ -1,6 +1,5 @@
import { Stripe } from '@stripe/stripe-js';
import {
hasErrors,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused


FormspreeContext.displayName = 'Formspree';

let stripePromise: Promise<Stripe>;
let stripePromise: Promise<Stripe | null>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix type: getStripe returns Promise<Stripe | null>

};

if (props.stripePK) {
getStripePromise();
getStripePromise(props.stripePK);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix type: need to pass the value after type narrowing

Comment on lines +53 to +54
let stripe: Stripe | null;
let elements: StripeElements | null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix type: the client getters for these return T | null.

@@ -171,6 +168,7 @@ const useForm = (
: undefined,
})
.then((result: SubmissionResponse) => {
// @ts-ignore: unhandled result.response is possibly null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to fix as many type errors without changing the runtime behavior. It's impossible to fix these without changing the implementation to handle null better. We'll do in a follow-up PR.

@@ -64,7 +64,7 @@ it('does not render anything if the field does not have an error', () => {
{
field: 'name',
message: 'is required',
code: 'REQUIRED',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix type: correct the enums.

turbo.json Outdated
Comment on lines 23 to 27
"topo": {
"dependsOn": ["^topo"]
},
"typecheck": {
"dependsOn": ["topo"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://turbo.build/repo/docs/core-concepts/monorepos/task-dependencies#dependencies-outside-of-a-task

Adding topo allows typecheck tasks in formspree-core and formspree-react to run in parallel while ensuring that changing in formspree-core will cause a cache miss in formspree-react.

An alternative is to do: "typecheck": { "dependsOn": ["^typecheck"] } but it'll cause formspree-react to always run after formspree-core.

@@ -1,4 +1,6 @@
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["tsconfig.json"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the root tsconfig.json changes, it should cause cache miss to the tasks.

@bhongy bhongy marked this pull request as ready for review June 19, 2023 08:37
@bhongy bhongy requested a review from colevscode June 19, 2023 08:38
Base automatically changed from chore/run-prettier-on-everything to main June 20, 2023 04:05
"declaration": true,
"declarationDir": "dist/types",
"noImplicitAny": true,
"moduleResolution": "node",
"declarationMap": true,
"jsx": "react",
Copy link
Contributor Author

@bhongy bhongy Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually, we should change this to react-jsx: https://stackoverflow.com/questions/67776707/ts-config-jsx-reactjsx-vs-react.

However, it'll produce a breaking change (generated typedef will use JSX type from react/runtime instead).

image

@@ -1,17 +1,15 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "@formspree/react",
"extends": ["../../tsconfig.json"],
"compilerOptions": {
"target": "es5",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed (i.e. to use root tsconfig value for consistency between core and react packages) in the future. Since it will produce a very different built index.js, we will update it as part of a breaking change release.

cc/ @colevscode

@colevscode
Copy link
Member

colevscode commented Jun 22, 2023

Might be worth a patch bump, if for no other reason, updates the changelog so we can track these improvements. To do that I suggest running yarn changeset and let the changeset bot do it's thing

@bhongy bhongy merged commit 07c30c5 into main Jun 22, 2023
@bhongy bhongy deleted the chore/manage-typescript-at-root branch June 22, 2023 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants