mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-05 20:32:42 +02:00
chore(v2): tighten up the TypeScript onboarding (#3244)
* Tighten up the TypeScript onboarding * Extend the API to cover everything used in the classic theme * Wrap up internal TS support
This commit is contained in:
parent
a4c8a7f55b
commit
33ecc4bb17
7 changed files with 65 additions and 22 deletions
|
@ -6,5 +6,10 @@
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/react": "*",
|
||||||
|
"@types/react-helmet": "*",
|
||||||
|
"@types/react-router-dom": "*"
|
||||||
|
},
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,6 +53,42 @@ declare module '@theme-original/*';
|
||||||
|
|
||||||
declare module '@docusaurus/*';
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
declare module '*.module.css' {
|
declare module '*.module.css' {
|
||||||
const classes: {readonly [key: string]: string};
|
const classes: {readonly [key: string]: string};
|
||||||
export default classes;
|
export default classes;
|
||||||
|
|
|
@ -127,4 +127,4 @@ class PendingNavigation extends React.Component<Props, State> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withRouter(PendingNavigation);
|
export default withRouter(PendingNavigation as any) as any;
|
||||||
|
|
|
@ -142,7 +142,7 @@ function Link({
|
||||||
{...props}
|
{...props}
|
||||||
onMouseEnter={onMouseEnter}
|
onMouseEnter={onMouseEnter}
|
||||||
innerRef={handleRef}
|
innerRef={handleRef}
|
||||||
to={targetLink}
|
to={targetLink || ''}
|
||||||
// avoid "React does not recognize the `activeClassName` prop on a DOM element"
|
// avoid "React does not recognize the `activeClassName` prop on a DOM element"
|
||||||
{...(isNavLink && {activeClassName})}
|
{...(isNavLink && {activeClassName})}
|
||||||
/>
|
/>
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
"lib": ["es2017","es2019.array", "DOM"],
|
"lib": ["es2017","es2019.array", "DOM"],
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"declarationMap": true,
|
"declarationMap": true,
|
||||||
|
"jsx": "react",
|
||||||
|
|
||||||
/* Strict Type-Checking Options */
|
/* Strict Type-Checking Options */
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
|
|
@ -5,37 +5,22 @@ title: TypeScript Support
|
||||||
|
|
||||||
## Setup
|
## 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
|
```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
|
||||||
```
|
```
|
||||||
|
|
||||||
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"
|
```json title="tsconfig.json"
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"extends": "@tsconfig/docusaurus/tsconfig.json",
|
||||||
"allowJs": true,
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"jsx": "react",
|
|
||||||
"lib": ["DOM"],
|
|
||||||
"noEmit": true,
|
|
||||||
"noImplicitAny": false
|
|
||||||
},
|
|
||||||
"include": ["src/"]
|
"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.
|
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.
|
||||||
|
|
||||||
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" />
|
|
||||||
```
|
|
||||||
|
|
||||||
This file makes TypeScript recognize various Docusaurus specific webpack aliases like `@theme`, `@docusaurus`, `@generated`.
|
|
||||||
|
|
||||||
Now you can start writing TypeScript theme components.
|
Now you can start writing TypeScript theme components.
|
||||||
|
|
||||||
|
|
16
yarn.lock
16
yarn.lock
|
@ -4120,6 +4120,13 @@
|
||||||
"@types/webpack" "*"
|
"@types/webpack" "*"
|
||||||
"@types/webpack-dev-server" "*"
|
"@types/webpack-dev-server" "*"
|
||||||
|
|
||||||
|
"@types/react-helmet@*":
|
||||||
|
version "6.1.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/react-helmet/-/react-helmet-6.1.0.tgz#af586ed685f4905e2adc7462d1d65ace52beee7a"
|
||||||
|
integrity sha512-PYRoU1XJFOzQ3BHvWL1T8iDNbRjdMDJMT5hFmZKGbsq09kbSqJy61uwEpTrbTNWDopVphUT34zUSVLK9pjsgYQ==
|
||||||
|
dependencies:
|
||||||
|
"@types/react" "*"
|
||||||
|
|
||||||
"@types/react-helmet@^6.0.0":
|
"@types/react-helmet@^6.0.0":
|
||||||
version "6.0.0"
|
version "6.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/@types/react-helmet/-/react-helmet-6.0.0.tgz#5b74e44a12662ffb12d1c97ee702cf4e220958cf"
|
resolved "https://registry.yarnpkg.com/@types/react-helmet/-/react-helmet-6.0.0.tgz#5b74e44a12662ffb12d1c97ee702cf4e220958cf"
|
||||||
|
@ -4144,6 +4151,15 @@
|
||||||
"@types/react" "*"
|
"@types/react" "*"
|
||||||
"@types/react-router" "*"
|
"@types/react-router" "*"
|
||||||
|
|
||||||
|
"@types/react-router-dom@*":
|
||||||
|
version "5.1.5"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/react-router-dom/-/react-router-dom-5.1.5.tgz#7c334a2ea785dbad2b2dcdd83d2cf3d9973da090"
|
||||||
|
integrity sha512-ArBM4B1g3BWLGbaGvwBGO75GNFbLDUthrDojV2vHLih/Tq8M+tgvY1DSwkuNrPSwdp/GUL93WSEpTZs8nVyJLw==
|
||||||
|
dependencies:
|
||||||
|
"@types/history" "*"
|
||||||
|
"@types/react" "*"
|
||||||
|
"@types/react-router" "*"
|
||||||
|
|
||||||
"@types/react-router@*":
|
"@types/react-router@*":
|
||||||
version "5.1.7"
|
version "5.1.7"
|
||||||
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-5.1.7.tgz#e9d12ed7dcfc79187e4d36667745b69a5aa11556"
|
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-5.1.7.tgz#e9d12ed7dcfc79187e4d36667745b69a5aa11556"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue