-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
chore(v2): tighten up the TypeScript onboarding #3244
Changes from 1 commit
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 |
---|---|---|
|
@@ -53,6 +53,28 @@ declare module '@theme-original/*'; | |
|
||
declare module '@docusaurus/*'; | ||
|
||
declare module '@docusaurus/Head' { | ||
const helmet: typeof import("react-helmet") | ||
export default helmet | ||
} | ||
|
||
declare module '@docusaurus/Link' { | ||
const Link: import("react-router-dom").Link | ||
export default Link | ||
} | ||
|
||
declare module '@docusaurus/router' { | ||
export const Redirect: (props: { to: string}) => import("react").Component | ||
} | ||
|
||
declare module '@docusaurus/useDocusaurusContext' { | ||
export default function(): any | ||
} | ||
|
||
declare module '@docusaurus/useBaseUrl' { | ||
export default function(relativePath: string): string | ||
} | ||
|
||
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 use the |
||
declare module '*.module.css' { | ||
const classes: {readonly [key: string]: string}; | ||
export default classes; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,37 +5,22 @@ title: TypeScript Support | |
|
||
## Setup | ||
|
||
Docusaurus supports writing and using TypeScript theme components. To start using TypeScript, add `@docusaurus/module-type-aliases` to your project: | ||
Docusaurus supports writing and using TypeScript theme components. To start using TypeScript, add `@docusaurus/module-type-aliases` and some `@types` dependencies to your project: | ||
|
||
```bash | ||
npm install --save-dev typescript @docusaurus/module-type-aliases | ||
npm install --save-dev typescript @docusaurus/module-type-aliases @types/react @types/react-router-dom @types/react-helmet | ||
``` | ||
slorber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Then add `tsconfig.json` to your project root with following content: | ||
Then add `tsconfig.json` to your project root with the following content: | ||
|
||
```json title="tsconfig.json" | ||
{ | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"esModuleInterop": true, | ||
"jsx": "react", | ||
"lib": ["DOM"], | ||
"noEmit": true, | ||
"noImplicitAny": false | ||
}, | ||
"extends": "@tsconfig/docusaurus/tsconfig.json", | ||
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. Moves all these config options to tsconfig/bases#27 - then it's obvious what is framework vs project |
||
"include": ["src/"] | ||
} | ||
``` | ||
|
||
Docusaurus doesn't use this `tsconfig.json` to compile your TypeScript. It is added just for a nicer Editor experience, although you can choose to run `tsc --noEmit` to type check your code for yourself. | ||
|
||
Then add `types.d.ts` in your `src` folder with the following content: | ||
|
||
```ts title="src/types.d.ts" | ||
/// <reference types="@docusaurus/module-type-aliases" /> | ||
``` | ||
|
||
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. Removed with https://github.com/tsconfig/bases/pull/28/files This has one side-effect of encouraging people to be explicit in their global types, so for example if they wanted to add Jest tests, it would need to look like:
But I imagine that this is the minority of folks, so I've left it off |
||
This file makes TypeScript recognize various Docusaurus specific webpack aliases like `@theme`, `@docusaurus`, `@generated`. | ||
Docusaurus doesn't use this `tsconfig.json` to compile your project. It is added just for a nicer Editor experience, although you can choose to run `tsc` to type check your code for yourself or on CI. | ||
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. The config specifies |
||
|
||
Now you can start writing TypeScript theme components. | ||
|
||
|
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.
This globally applies an
any
to all@docusaurus/*
stuff, but because the following are more specific, then they get picked up as the module definition. This means as you add new things to the module TS users just get any, but then it can get filled in here.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.