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 typescript support and migrate function example #782

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marlonkeating
Copy link
Contributor

Jira Ticket

This change adds TypeScript support to the code base, and gives a case study in migrating Javascript to TypeScript one function at a time.

For all changes

  • Ensure adequate tests are in place (or reviewed existing tests cover changes)

Only if submitting a visual change

  • Ensure to attach screenshots
  • Ensure to have UX team confirm screenshots

@codecov
Copy link

codecov bot commented Jun 27, 2023

Codecov Report

Patch coverage: 84.32% and project coverage change: -0.04 ⚠️

Comparison is base (d36b78a) 84.87% compared to head (807433e) 84.83%.

❗ Current head 807433e differs from pull request most recent head a6a3665. Consider uploading reports for the commit a6a3665 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #782      +/-   ##
==========================================
- Coverage   84.87%   84.83%   -0.04%     
==========================================
  Files         320      322       +2     
  Lines        6399     6521     +122     
  Branches     1552     1578      +26     
==========================================
+ Hits         5431     5532     +101     
- Misses        941      960      +19     
- Partials       27       29       +2     
Impacted Files Coverage Δ
.../components/course/course-header/CourseRunCard.jsx 100.00% <ø> (ø)
...ourse-header/data/hooks/useCourseRunCardAction.jsx 100.00% <ø> (ø)
...e/course-header/data/hooks/useCourseRunCardData.js 100.00% <ø> (ø)
src/components/course/data/service.js 46.51% <ø> (ø)
...rse/enrollment/components/ToDataSharingConsent.jsx 100.00% <ø> (ø)
.../course/enrollment/components/ToEcomBasketPage.jsx 75.00% <ø> (-1.93%) ⬇️
...nt/components/ToExecutiveEducation2UEnrollment.jsx 50.00% <0.00%> (ø)
src/components/course/enrollment/constants.js 100.00% <ø> (ø)
...xecutive-education-2u/ExecutiveEducation2UPage.jsx 100.00% <ø> (ø)
...n-2u/components/EnrollmentCompletedSummaryCard.jsx 100.00% <ø> (ø)
... and 36 more

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

.eslintignore Outdated
src/segment.jsx/
utils.jsx
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason, course/data/utils.jsx and only this file says that it is not included under the tsconfig file.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renaming utils.tsx fixed the issue. Props to @davidjoy for suggesting this might be the issue!

@@ -15,7 +15,7 @@ MenuTrigger.defaultProps = {
tag: 'div',
className: null,
};
const MenuTriggerType = <MenuTrigger />.type;
const MenuTriggerType = (<MenuTrigger />).type;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

typescript-eslint called this out while the old configuration didn't, for some reason.

image

const startDate = new Date(start);
return startDate && today >= startDate;
}
import { compareRedeemableOffers, hasCourseStarted as hasCourseStartedTs} from './utils.tsx';

Choose a reason for hiding this comment

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

Is it actually necessary to add the extensions now? I wonder if we could tweak the webpack config in frontend-build to avoid this. I know I have a (personal) TS project where I don't have to include extensions on my imports. 🤔

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 we don't have to disambiguate between TypeScript and a Javascript file with identical file paths, then we don't need to include a filename extension.

@@ -88,8 +88,9 @@
"build": "fedx-scripts webpack",
"build:with-theme": "THEME=npm:@edx/brand-edx.org@latest npm run install-theme && fedx-scripts webpack",
"i18n_extract": "BABEL_ENV=i18n fedx-scripts babel src --quiet > /dev/null",
"lint": "fedx-scripts eslint --ext .js --ext .jsx .",
"lint:fix": "fedx-scripts eslint --fix --ext .js --ext .jsx .",
"check-types": "tsc --noemit",

Choose a reason for hiding this comment

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

Wonder if we could delegate this to fedx-scripts like most of the other scripts here. It makes it easier to make changes across MFEs if the details of the build change in frontend-build.


## Decision

We will prioritize using TypeScript in the following places:

Choose a reason for hiding this comment

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

Two thoughts:

We already have support for typescript in frontend-build; I don't see a corresponding ADR there. I like the idea of moving this ADR into frontend-build to explain why we added typescript after advocating against it for a number of years. We've already made the choice to go for it more systemically, adding typescript to this repo may not even be ADR worthy unless there are some specific reasons we want to be early adopters for this repo.

Related to this specific "Decision" section - My feedback here is that I've found the most utility in using typescript in data/API/state layers, and it feels less useful in React components because it tends to get really complain-y and requires you to annotate all the internals of React for very little benefit. I assume most of our react components are already using PropTypes, too, which gives us some level of safety/checking there. If it were me I might write some of that nuance into this ADR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@davidjoy
Copy link

In general, exciting to see Typescript getting added to our toolbelt. I'm somewhat opinionated about using it responsibly, and feel like we might benefit from an ADR about it outside this repo. I doubt we'll all be able to agree on "when to TS", but having a statement in frontend-build about the fact that we're using it at all now would be good.

build: rename ts file to fix duplicate js/ts filename issues
@marlonkeating marlonkeating marked this pull request as ready for review June 28, 2023 23:17
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