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

Introducing TypeScript best practices #403

Merged
merged 18 commits into from
Jun 6, 2024
Merged

Introducing TypeScript best practices #403

merged 18 commits into from
Jun 6, 2024

Conversation

nicholasio
Copy link
Member

This PR introduces a new TypeScript section and its best practices.

Copy link
Contributor

@pattonwebz pattonwebz left a comment

Choose a reason for hiding this comment

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

I spotted some typos in the new docs that I created suggestions for.

_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved

Note: we recommend setting the `noImplicitAny` setting to `true` in `tsconfig.json` which forces explicitly setting a type as any. This makes it easier to identify code that is relying on any and would also make catching the escape hatch in code reviews which is a great opportunity to get feedback and potentially get the types right.

#### 3. The `// @ts-expect-error` comment
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally this would be avoided if possible - casting as any should nearly always fix this issue. If you're having to use this - it may be a case where the engineer could ask for someone more experienced with TS to assist with the issue. Would also work as a great learning experience.

Copy link
Member

Choose a reason for hiding this comment

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

In my experience the easiest way to assist with TS types for other engineers is once it's in a PR and I can see all the code. I've found Slack messages in isolation without seeing all the code hard to answer. So perhaps encourage them to get their work done with any and then TS types support comes once the PR is open. It also leaves a feedback trail for any other engineers on the project

Copy link
Member Author

Choose a reason for hiding this comment

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

@kasparszarinovs I agree but I also feel like we need this as a last resort. Like @tobeycodes said this could potentially be a easy fix during code review.

_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved
Copy link
Member

@Antonio-Laguna Antonio-Laguna left a comment

Choose a reason for hiding this comment

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

@nicholasio great stuff! I just stumbled upon this and thought I'd give it a read! Found a few minor typos and left a comment for your consideration.

Great work!

_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Show resolved Hide resolved
// component logic
}
```
**Note**: If using React < 17, be aware that `children` will be explicitly set for all components using `React.FC`. This can cause some TS errors when upgrading React to 18+, if a component is passed `children` but does not explicitly declare it as a prop.
Copy link
Member

Choose a reason for hiding this comment

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

While this is true, I'd say it's been a while since React 18 is out (2 years!) While it doesn't harm I don't feel this is as relevant now? Just flagging to avoid this feeling dated when it's born but feel free to disregard

Copy link
Member Author

Choose a reason for hiding this comment

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

The only reason I added this was bc there might be projects still on React 17.

Copy link
Member

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks @nicholasio! I found a few more easy fixes with suggestions.

_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
_includes/markdown/Typescript.md Outdated Show resolved Hide resolved
@nicholasio nicholasio merged commit 627db72 into gh-pages Jun 6, 2024
3 checks passed
@nicholasio nicholasio deleted the typescript branch June 6, 2024 16:21
};

// If Layout is not passed a children, TS will catch the error
const Layout = (props: LayoutProps) => {
Copy link

Choose a reason for hiding this comment

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

@nicholasio should this be rewritten using React.FC as recommended above?

Copy link
Member Author

Choose a reason for hiding this comment

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

Made a quick follow-up commit to update this thanks

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.

10 participants