-
-
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 all commits
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,42 @@ declare module '@theme-original/*'; | |
|
||
declare module '@docusaurus/*'; | ||
|
||
declare module '@docusaurus/Head' { | ||
const helmet: typeof import('react-helmet').Helmet; | ||
export default helmet; | ||
} | ||
|
||
declare module '@docusaurus/Link' { | ||
type RRLinkProps = Partial<import('react-router-dom').LinkProps>; | ||
type LinkProps = RRLinkProps & { | ||
to?: string; | ||
activeClassName?: string; | ||
isNavLink?: boolean; | ||
}; | ||
const Link: (props: LinkProps) => JSX.Element; | ||
export default Link; | ||
} | ||
|
||
declare module '@docusaurus/router' { | ||
export const Redirect: (props: {to: string}) => import('react').Component; | ||
export function matchPath( | ||
pathname: string, | ||
opts: {path?: string; exact?: boolean; strict?: boolean}, | ||
): boolean; | ||
export function useLocation(): Location; | ||
} | ||
|
||
declare module '@docusaurus/useDocusaurusContext' { | ||
export default function (): any; | ||
} | ||
|
||
declare module '@docusaurus/useBaseUrl' { | ||
export default function ( | ||
relativePath: string | undefined, | ||
opts?: {absolute?: true; forcePrependBaseUrl?: true}, | ||
): 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 |
---|---|---|
|
@@ -127,4 +127,4 @@ class PendingNavigation extends React.Component<Props, State> { | |
} | ||
} | ||
|
||
export default withRouter(PendingNavigation); | ||
export default withRouter(PendingNavigation as any) as any; | ||
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. introducing It previously was an any |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,7 +142,7 @@ function Link({ | |
{...props} | ||
onMouseEnter={onMouseEnter} | ||
innerRef={handleRef} | ||
to={targetLink} | ||
to={targetLink || ''} | ||
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.
|
||
// avoid "React does not recognize the `activeClassName` prop on a DOM element" | ||
{...(isNavLink && {activeClassName})} | ||
/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"lib": ["es2017","es2019.array", "DOM"], | ||
"declaration": true, | ||
"declarationMap": true, | ||
"jsx": "react", | ||
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. optional and doesn't affect the output to my knowledge, but I get errors everywhere in vscode without it |
||
|
||
/* Strict Type-Checking Options */ | ||
"strict": true, | ||
|
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 @tsconfig/docusaurus | ||
``` | ||
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.