-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feature(gatsby): Support TypeScript by default by compiling TS files #23547
Changes from 12 commits
183c3fd
d548ed0
d706214
762e585
41b1354
d3561ab
d804992
9e760f8
f7d5704
f50c61a
5d293d1
2d79b05
4e89725
2e74c8e
3a5795f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
title: TypeScript and Gatsby | ||
--- | ||
|
||
## Introductory paragraph | ||
|
||
[TypeScript](https://www.typescriptlang.org/) is a JavaScript superset which extends the language to include type definitions allowing codebases to be statically checked for soundness. Gatsby provides an integrated experience out of the box, similar to an IDE. If you are new to TypeScript, adoption can _and should_ be incremental. Since Gatsby natively supports JavaScript and TypeScript, you can change files from .js to .tsx at any point to start adding types and gaining the benefits of a type system. | ||
|
||
## Example | ||
|
||
blainekasten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```tsx:title=src/pages/index.js | ||
import React from "react" | ||
import { PageProps } from "gatsby" | ||
|
||
export default function IndexRoute(props: PageProps) { | ||
return ( | ||
<> | ||
<h1>Path:</h1> | ||
<p>{props.path}</p> | ||
</> | ||
) | ||
} | ||
``` | ||
|
||
In this example, we are using the power of TypeScript, in combination with exported types from Gatsby (`PageProps`) to tell this code what props is. This can greatly improve your developer experience by letting your IDE show you what properties are injected by Gatsby. To see all of the types available look at our [TypeScript definition file](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/index.d.ts). | ||
|
||
## Other resources | ||
|
||
TypeScript integration is supported through automatically including [`gatsby-plugin-typescript`](/plugins/gatsby-plugin-typescript). Visit that link to see configuration options and limitations of this setup. | ||
|
||
If you are new to TypeScript, check out these other resources to learn more: | ||
|
||
- [TypeScript Documentation](https://www.typescriptlang.org/docs/handbook/basic-types.html) | ||
- [TypeScript Playground (Try it out!)](https://www.typescriptlang.org/play/index.html) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gatsby's API is available in the Playground, so this could be more like this link instead |
||
- [TypeScript Gatsby Example](https://using-typescript.gatsbyjs.org/) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,4 +71,97 @@ describe(`Load plugins`, () => { | |
|
||
expect(plugins).toMatchSnapshot() | ||
}) | ||
|
||
describe(`TypeScript support`, () => { | ||
it(`loads gatsby-plugin-typescript if not provided`, async () => { | ||
const config = { | ||
plugins: [], | ||
} | ||
|
||
let plugins = await loadPlugins(config) | ||
|
||
plugins = replaceFieldsThatCanVary(plugins) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to create a different helper function here? The one that exists now also check paths property and might cause weird test failures when we might move more plugins as a default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure. Do you have any suggestions? It seems to work right now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's the same setup we have for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe But this doesn't seem to be concern of this pull request? |
||
|
||
expect(plugins).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
browserAPIs: [], | ||
id: ``, | ||
name: `gatsby-plugin-typescript`, | ||
nodeAPIs: [ | ||
`resolvableExtensions`, | ||
`onCreateBabelConfig`, | ||
`onCreateWebpackConfig`, | ||
], | ||
pluginOptions: { | ||
plugins: [], | ||
}, | ||
resolve: ``, | ||
ssrAPIs: [], | ||
version: `1.0.0`, | ||
}), | ||
]) | ||
) | ||
}) | ||
|
||
it(`uses the user provided plugin-typescript if provided`, async () => { | ||
const config = { | ||
plugins: [ | ||
{ | ||
resolve: `gatsby-plugin-typescript`, | ||
options: { | ||
jsxPragma: `h`, | ||
}, | ||
}, | ||
], | ||
} | ||
|
||
let plugins = await loadPlugins(config) | ||
|
||
plugins = replaceFieldsThatCanVary(plugins) | ||
|
||
expect(plugins).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
browserAPIs: [], | ||
id: ``, | ||
name: `gatsby-plugin-typescript`, | ||
nodeAPIs: [ | ||
`resolvableExtensions`, | ||
`onCreateBabelConfig`, | ||
`onCreateWebpackConfig`, | ||
], | ||
pluginOptions: { | ||
plugins: [], | ||
jsxPragma: `h`, | ||
}, | ||
resolve: ``, | ||
ssrAPIs: [], | ||
version: `1.0.0`, | ||
}), | ||
]) | ||
) | ||
blainekasten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
it(`does not add gatsby-plugin-typescript if it exists in config.plugins`, async () => { | ||
const config = { | ||
plugins: [ | ||
`gatsby-plugin-typescript`, | ||
{ resolve: `gatsby-plugin-typescript` }, | ||
], | ||
} | ||
|
||
let plugins = await loadPlugins(config) | ||
|
||
plugins = replaceFieldsThatCanVary(plugins) | ||
|
||
const tsplugins = plugins.filter( | ||
plugin => plugin.name === `gatsby-plugin-typescript` | ||
) | ||
|
||
// TODO: I think we should probably be de-duping, so this should be 1. | ||
// But this test is mostly here to ensure we don't add an _additional_ gatsby-plugin-typescript | ||
pieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(tsplugins.length).toEqual(2) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 Yes! This hits all the points I had in mind!