Skip to content

[No QA] Add TypeScript Guideline#21549

Merged
hayata-suenaga merged 20 commits into
mainfrom
hayata-addTSGuideline
Jul 24, 2023
Merged

[No QA] Add TypeScript Guideline#21549
hayata-suenaga merged 20 commits into
mainfrom
hayata-addTSGuideline

Conversation

@hayata-suenaga

@hayata-suenaga hayata-suenaga commented Jun 26, 2023

Copy link
Copy Markdown
Contributor

Built upon the guideline PR by @fabioh8010 and @blazejkustra. Further iteration on #21050

$ #20623

NO QA/TEST

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web

N/A

Mobile Web - Chrome

N/A

Mobile Web - Safari

N/A

Desktop

N/A

iOS

N/A

Android

N/A

@hayata-suenaga hayata-suenaga mentioned this pull request Jun 26, 2023
55 tasks
@hayata-suenaga hayata-suenaga marked this pull request as ready for review June 26, 2023 08:38
@hayata-suenaga hayata-suenaga requested a review from a team as a code owner June 26, 2023 08:38
@melvin-bot melvin-bot Bot requested review from stitesExpensify and removed request for a team June 26, 2023 08:39
@melvin-bot

melvin-bot Bot commented Jun 26, 2023

Copy link
Copy Markdown

@stitesExpensify Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_CHEATSHEET.md Outdated
Comment thread contributingGuides/TS_CHEATSHEET.md Outdated
@blazejkustra

Copy link
Copy Markdown
Contributor

We lack couple of sections from the original guidelines:

  • File structure (reusable type definitions)
  • File Extensions (d.ts files are mentioned, how about tsx?)
  • Platform-Specific Variants (we definitely need that, right?)
  • Const assertions (NAB but, quite useful to type global-level constants)

What do you think, should we include them here? @hayata-suenaga @fabioh8010

@blazejkustra

blazejkustra commented Jun 26, 2023

Copy link
Copy Markdown
Contributor

You can check how these markdown files look like in this test repo..

fyi @hayata-suenaga @fabioh8010 You can view it directly in this repo, the easiest way is to click on the file options -> "View file"

image

Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment on lines +274 to +297
<a name="convension-export-prop-types"></a><a name="1.14"></a>

- [1.14](#convension-export-prop-types) **Prop Types**: Define and export prop types for components. Use exported prop types instead of grabbing the prop type from a component.

> Why? Exporting prop types aids reusability.

```tsx
// MyComponent.tsx
export type MyComponentProps = {
foo: string;
};

export default function MyComponent({ foo }: MyComponentProps) {
return <Text>{foo}</Text>;
}

// bad
import { ComponentProps } from "React";
import MyComponent from "./MyComponent";
type MyComponentProps = ComponentProps<typeof MyComponent>;

// good
import MyComponent, { MyComponentProps } from "./MyComponent";
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@hayata-suenaga Are we going to always export the component's props, even when isn't necessary? cc @blazejkustra

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When props are only used inside of the file where it is declared I wouldn't export it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

From previous guidelines PR:

Types specific to a single file should remain inside that file to keep their scope limited and their usage clear.

@hayata-suenaga hayata-suenaga Jun 27, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When props are only used inside the file where it is declared, I wouldn't export it.

I was thinking it might be a good idea to always export a prop type if its component is also exported. We don't know if other components might need the prop type or not

what do you think @fabioh8010 @blazejkustra

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't have a strong opinion here but, it can lead to a sort of "premature optimization". We should not export props just in case they might be needed in the future (YAGNI principle).

Instead, if such need arises where props are required in another component, we can start exporting them at that time. @hayata-suenaga

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would prefer to not export every prop type as they are going to "pollute" the editor's autocomplete mechanisms unnecessarily, we should export only things that are going to be used across the project.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay that makes sense removing this part. should we allow the usage of type MyComponentProps = ComponentProps<typeof MyComponent>; or if the prop type is needed, should they be exported?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@hayata-suenaga @blazejkustra I think it's better to export the prop type itself when needed instead of using ComponentProps 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay then let's include instructions to export and import prop types if you find yourself trying to use ComponentProps

what do you think @blazejkustra

Comment thread contributingGuides/TS_CHEATSHEET.md Outdated
Comment thread contributingGuides/TS_CHEATSHEET.md Outdated
Comment thread contributingGuides/TS_CHEATSHEET.md Outdated
Comment thread contributingGuides/TS_CHEATSHEET.md Outdated
@fabioh8010

Copy link
Copy Markdown
Contributor

@hayata-suenaga I agree with @blazejkustra about the missing sections. Is there a reason for removing them or are you still working on this PR?

@neil-marcellini neil-marcellini marked this pull request as draft June 26, 2023 16:27
@neil-marcellini

Copy link
Copy Markdown
Contributor

I'm marked this as a draft since @hayata-suenaga's latest comment on Slack says he still needs to add some pieces.

@hayata-suenaga

Copy link
Copy Markdown
Contributor Author

@fabioh8010 @blazejkustra thank you for your quick review 👍

I still have mission sections. I added a TODO list in the initial message of this PR, and I'll update them as I go

Comment thread contributingGuides/TS_STYLE.md
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment on lines +274 to +297
<a name="convension-export-prop-types"></a><a name="1.14"></a>

- [1.14](#convension-export-prop-types) **Prop Types**: Define and export prop types for components. Use exported prop types instead of grabbing the prop type from a component.

> Why? Exporting prop types aids reusability.

```tsx
// MyComponent.tsx
export type MyComponentProps = {
foo: string;
};

export default function MyComponent({ foo }: MyComponentProps) {
return <Text>{foo}</Text>;
}

// bad
import { ComponentProps } from "React";
import MyComponent from "./MyComponent";
type MyComponentProps = ComponentProps<typeof MyComponent>;

// good
import MyComponent, { MyComponentProps } from "./MyComponent";
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

From previous guidelines PR:

Types specific to a single file should remain inside that file to keep their scope limited and their usage clear.

Comment thread contributingGuides/TS_CHEATSHEET.md
Comment thread contributingGuides/TS_STYLE.md Outdated
@blazejkustra

blazejkustra commented Jun 27, 2023

Copy link
Copy Markdown
Contributor

I went through the previous PR and compared it to @hayata-suenaga version. Added couple of suggestions on what we are missing and would be awesome to include it. I want to make these TS conventions as clear as possible to contributors. cc @hayata-suenaga @fabioh8010

mountiny
mountiny previously approved these changes Jul 17, 2023

@mountiny mountiny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's do this thing!

@roryabraham roryabraham left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couple questions/clarifications, otherwise LGTM 👍🏼

Comment thread contributingGuides/TS_STYLE.md Outdated
function MyComponent({
requiredProp,
optionalPropWithDefaultValue = 42,
optionalProp,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should all optional props be required to have a default?

I don't feel strongly either way, but we should clarify that here because currently all optional props are required to have a default.

@hayata-suenaga hayata-suenaga Jul 17, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

agree we should clarify on this. if you don't pass anything to an optional prop and a default value is not provided, the value for that prop is undefined.

If the prop can be undefined and the component implementation can handle that situation, then we don't have to require default props to be passed always.

in other words, if the default value is undefined (or null), default value is not needed imo

I gonna post this to ask if everyone feels okay changing the rule in the open source channel as this changes the current guideline

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree with your suggestion

@neil-marcellini neil-marcellini left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking great 🚀

Comment thread contributingGuides/PROPTYPES_CONVERSION_TABLE.md Outdated
Comment thread contributingGuides/PROPTYPES_CONVERSION_TABLE.md Outdated
Comment thread contributingGuides/PROPTYPES_CONVERSION_TABLE.md

@fabioh8010 fabioh8010 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@hayata-suenaga Some nitpicks but overall is looking great, approved 🚀

Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated

> Refer to [the propTypes Migration Table](./PROPTYPES_CONVERSION_TABLE.md) on how to type props based on existing `propTypes`.

> Assign a default value to each optional prop unless the default values is `undefined` or `null`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@hayata-suenaga Please see my comment on Slack regarding null.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I ended up removing null from the text and made it

unless the default values is `undefined`

putting null here might cause confusion and there might be code that checks for if (prop===null)

Comment thread contributingGuides/TS_STYLE.md
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/TS_STYLE.md Outdated
Comment thread contributingGuides/PROPTYPES_CONVERSION_TABLE.md Outdated
Comment thread contributingGuides/PROPTYPES_CONVERSION_TABLE.md Outdated
Comment thread contributingGuides/PROPTYPES_CONVERSION_TABLE.md Outdated

@neil-marcellini neil-marcellini left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking good, thanks for making the changes I suggested. I agree with Fábio's comments too.

roryabraham
roryabraham previously approved these changes Jul 20, 2023
@hayata-suenaga

Copy link
Copy Markdown
Contributor Author

thank you everyone for detailed reviews 🙇 if this PR looks good, could someone do a PR reviewer checklist please 🙇

@neil-marcellini neil-marcellini changed the title Add TypeScript Guideline [No QA] Add TypeScript Guideline Jul 20, 2023
@neil-marcellini

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

No videos, this is a markdown change only.

Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

@neil-marcellini

Copy link
Copy Markdown
Contributor

@hayata-suenaga you can include an author checklist to make the checks pass or we can just merge with failing checks and explain why.

@WoLewicki WoLewicki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Left some typo comments.

Comment thread contributingGuides/PROPTYPES_CONVERSION_TABLE.md Outdated
Comment thread contributingGuides/TS_CHEATSHEET.md Outdated
Comment thread contributingGuides/TS_CHEATSHEET.md
@hayata-suenaga hayata-suenaga merged commit 2d0547e into main Jul 24, 2023
@hayata-suenaga hayata-suenaga deleted the hayata-addTSGuideline branch July 24, 2023 21:13
@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/hayata-suenaga in version: 1.3.45-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/mountiny in version: 1.3.45-7 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

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.

8 participants