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

Convert packages/react storybook to typescript. Convert checkbox story to tsx #12000

Closed
wants to merge 28 commits into from

Conversation

mbarrer
Copy link
Contributor

@mbarrer mbarrer commented Aug 23, 2022

Closes #11587

Changes have been made to allow the use of typescript within packages/react so that storybook stories can start to be written in typescript. As part of this change, the main.js for storybook has now been converted to main.ts and has some initial typings. Typing files have been added for Checkbox that we're sourced from the DefinitelyTyped repo and adjusted for carbon v11

Changelog

New

  • "typescript": "^4.7.4"
  • "@typescript-eslint/eslint-plugin": "^5.30.5" and "@typescript-eslint/parser": "^5.34.0" for linting
  • "ts-node": "^10.9.1" for supporting imports in storybook main.ts
  • typings/shared.d.ts common types file pulled from DefinitielyTyped
  • components/Checkbox/Checkbox.d.ts new typing file for Checkbox (adjusted DT typings)
  • custom.d.ts ambient module to allow imports of .mdx files in .tsx
  • tsconfig.json

Changed

  • update mini-css-extract-plugin to support native ts typings
  • converted .storybook/main.js to typescript and allow stories to be written in typescript
  • converted components/Checkbox/Checkbox.stories.js to tsx using the new typings
  • Added linting for .ts, .tsx to eslint-config-carbon/plugins/react.js

Testing / Reviewing

In order to test:

  • fresh yarn install
  • new build
  • launch storybook and verify that the Checkbox story is working and has the same functionality as it had before

@mbarrer mbarrer requested a review from a team as a code owner August 23, 2022 16:51
@github-actions
Copy link
Contributor

github-actions bot commented Aug 23, 2022

DCO Assistant Lite bot All contributors have signed the DCO.

@netlify
Copy link

netlify bot commented Aug 23, 2022

Deploy Preview for carbon-components-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit cfef450
🔍 Latest deploy log https://app.netlify.com/sites/carbon-components-react/deploys/6328a237aeaec6000847ed06
😎 Deploy Preview https://deploy-preview-12000--carbon-components-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 23, 2022

Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit cfef450
🔍 Latest deploy log https://app.netlify.com/sites/carbon-elements/deploys/6328a237f4719800083d252e
😎 Deploy Preview https://deploy-preview-12000--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@mbarrer
Copy link
Contributor Author

mbarrer commented Aug 23, 2022

I have read the DCO document and I hereby sign the DCO.

@tay1orjones tay1orjones requested a review from a team August 23, 2022 17:57
packages/react/src/typings/shared.d.ts Outdated Show resolved Hide resolved
@tay1orjones
Copy link
Member

tay1orjones commented Aug 23, 2022

@mbarrer Are you using Yarn v1 by chance? The project is configured to need yarn@3.2.1. I believe this is why the deploy previews are failing. I think if you update/install to v3.2.1 and re-run yarn install from the root things should start working.

Since you added new dependencies there should also be some additions to .yarn/cache as well for you to commit and push up in addition to the yarn.lock

@tay1orjones tay1orjones requested a review from a team August 23, 2022 20:18
@mbarrer
Copy link
Contributor Author

mbarrer commented Aug 23, 2022

Ah yes I have yarn 1.22.18 installed, I can update this on my end and run a fresh yarn install. I didnt see any .yarn/cache changes but that may just be because my version is so old

@tay1orjones
Copy link
Member

@mbarrer Hey so out of curiosity I pulled down this PR, reset yarn.lock to what's currently in main, and then re-ran yarn install. This pulled the changed files down from ~1800+ to just 26. I think the snafu with yarn v1 inadvertently reset all the hashes on the yarn cache, which is why it was such a huge change set.

Anyway, should be fixed now 👍

const babelLoader = config.module.rules.find((rule) => {
return rule.use.some(({ loader }) => {
// TODO: Fix typings for this function
webpack(config: any) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If statically typing this is desired, there are two options:

  1. Only use Configuration type from Webpack
import type { Configuration } from 'webpack';
...
webpack(config: Configuration) {
  1. Type the entire config using StorybookConfig
import type { StorybookConfig } from '@storybook/core-common';

const config: StorybookConfig = { /** */ }

module.exports = config;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep nice call, there was a few other changes needed in the body of the config to account for the union types used in the webpackFinal prop. The use of the typings also caught the fact that the webpack property should have actually been renamed to webpackFinal according to the storybook 6.5 docs

@mbarrer
Copy link
Contributor Author

mbarrer commented Aug 26, 2022

Looks like the build task also needs to be adjusted to output a main.js file for use in netlify, will be taking a look at this

Copy link
Contributor

@erifsx erifsx left a comment

Choose a reason for hiding this comment

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

This looks good to me.

I was concerned about what would happen if we released this without all the other types, or at least empty export declare let XX = unknown declarations for everything so I looked in the compiled lib and es folders and it doesn't look like it's actually shipping the types there yet and carbon doesn't ship the src folder. When we want to "go live" with the feature, I believe they'll need to be copied to the appropriate place along with an index.d.ts file.

Given all that, this looks like a great way to dark release our work.

@mbarrer
Copy link
Contributor Author

mbarrer commented Aug 30, 2022

Just letting you guys know that I haven't forgot about this PR, have just been busy this week with story work. I'll aim to have the build issue resolved by the end of the week

@@ -4,19 +4,17 @@
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs';
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 it would be a good practice to put node: before using each node core modules/lib, etc.

example:

import fs from 'node:fs' fs Docs
import path from 'node:path' Path docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh cool I didnt know you could reference these like this. good to know!

@mbarrer
Copy link
Contributor Author

mbarrer commented Sep 2, 2022

Seems like the issue I was hitting was something that @erifsx brought up in a thread on the workgroup slack. We found that we didnt need ts-node to get things running initially but for some reason this is now required in order to run the main.ts. Adding this to the dev dependancies seems to resolve the issues with netlify

@dakahn dakahn removed their request for review September 8, 2022 21:24
Comment on lines 47 to 51
{
files: ['*.ts', '*.tsx'],
plugins: ['@typescript-eslint'],
extends: ['plugin:@typescript-eslint/recommended'],
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Assuming carbon is using the no-unused-vars JS rule, you'll want to turn that off in favor of the typescript-eslint one. Here's an example

@@ -0,0 +1,7 @@
import { FunctionComponent, HTMLAttributes } from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing copyright statement

@@ -0,0 +1,52 @@
import * as React from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing copyright statement

@@ -0,0 +1,5 @@
import Checkbox from './Checkbox';
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing copyright statement

@@ -1,3 +1,4 @@
/* eslint-disable jest/no-standalone-expect */
Copy link
Contributor

Choose a reason for hiding this comment

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

Which part of this file is violating this rule? Should it instead be updated to conform to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm not sure why this was added, removing does not seem to cause any warnings to show

@@ -0,0 +1,5 @@
// Ambient module to allow the importing of .mdx files in storybook .tsx files
declare module '*.mdx' {
const content: any;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should definitely aim for zero warnings. This should either be unknown or if working around the lack of explicit any is too onerous to devs (since this is a gradual conversion), perhaps we consider disabling the no-explicit-any rule to start.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, i should have changed this to unknown

@mbarrer
Copy link
Contributor Author

mbarrer commented Sep 19, 2022

@jdharvey-ibm updated PR based off your recent comments

@tay1orjones
Copy link
Member

Closing in favor of #12222

@tay1orjones tay1orjones closed this Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Support TypeScript in our Storybook to set a baseline for types
7 participants