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

How is typescript automatically 'included' #26761

Closed
MathiasKandelborg opened this issue Sep 2, 2020 · 4 comments
Closed

How is typescript automatically 'included' #26761

MathiasKandelborg opened this issue Sep 2, 2020 · 4 comments
Labels
good first issue Issue that doesn't require previous experience with Gatsby help wanted Issue with a clear description that the community can help with. type: documentation An issue or pull request for improving or updating Gatsby's documentation

Comments

@MathiasKandelborg
Copy link

Summary

How do I control how Typescript is included? How does one incorporate typings into the developer experience, and why can one not have globals? E.g., React as global so one won't have to import it everywhere.

Relevant information

Typescript is 'included', but there's no information on how to control versions or what the default configuration is, or how one would go about changing it.

Also, please add information on the Typescript page to direct people towards using @types/my-package, especially for react and react-dom. There's no reason not to include the very first and essential steps you need to use TS with gatsby. Installing Typescript, then the vital types; you can't just install Typescript and go. You need the react types for starters.

@MathiasKandelborg MathiasKandelborg added the type: question or discussion Issue discussing or asking a question about Gatsby label Sep 2, 2020
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 2, 2020
@wardpeet
Copy link
Contributor

wardpeet commented Sep 2, 2020

I agree that our https://www.gatsbyjs.com/docs/typescript/ doesn't explain well what the requirements are. We love getting PRs to improve our documentation. If you encountered this problem, others would have too.

Typescript conversion is done by babel, not by the typescript compiler. It means no actual type checking is done. Sadly, npm does not enable us to add optional peerDependencies that show proper warnings that you might need @types/react, @types/react-dom and typescript. These should be documented in the linked document.

You can change "typescript configuration" by adding a tsconfig file like you normally would with any typescript project.

Why do you need to include React each time? Well, it's how react wants it. This changes inside react@17, where you'll be able to omit it completely. Another option is to add a customBabelrc file and enable a plugin like https://www.npmjs.com/package/babel-plugin-jsx-imports to do it for you.

@wardpeet wardpeet added good first issue Issue that doesn't require previous experience with Gatsby help wanted Issue with a clear description that the community can help with. type: documentation An issue or pull request for improving or updating Gatsby's documentation and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer type: question or discussion Issue discussing or asking a question about Gatsby labels Sep 2, 2020
@MathiasKandelborg
Copy link
Author

Oh, I got confused about configuration because of how TS is compiled with Gatsby.

I didn't run into any issues personally, I just expected there to be a oneliner to copy.

One builds react applications with GatsbyJS right? Essentially, you could say that React should be available in files in a React project right?

Gatsby already has a default babel configuration, why does anyone have to enable something that should be a default?

@MathiasKandelborg
Copy link
Author

There might also be a whole tutorial section to be had on TS.

There's already a bunch of available types, why not disclose that and give some examples on when to use them? Really get people started with safer coding!

@MathiasKandelborg
Copy link
Author

I needed a few more plugins for babel to work. Here's a working example, and damn am I happy it works!

yarn add -D / npm install --dev babel-plugin-jsx-imports @babel/preset-react @babel/plugin-syntax-jsx @babel/plugin-class-properties
module.exports = {
  presets: ['@babel/preset-react'],
  plugins: [
    '@babel/plugin-proposal-class-properties',
    'babel-plugin-jsx-imports',
    '@babel/plugin-syntax-jsx'
  ]
}

There is absolutely no point in importing React all over the place when you got babel. It should be the default in a React application that React is present. Make sure it is present in all jsx files once, and we're good to go. There's no real point to importing React everywhere or choosing to make that the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue that doesn't require previous experience with Gatsby help wanted Issue with a clear description that the community can help with. type: documentation An issue or pull request for improving or updating Gatsby's documentation
Projects
None yet
Development

No branches or pull requests

2 participants