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

chore(v2): tighten up the TypeScript onboarding #3244

Merged
merged 3 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ declare module '@theme-original/*';

declare module '@docusaurus/*';
Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen Shot 2020-08-08 at 11 26 02 AM


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
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the import("x") syntax, which means recommending people add the @types/x for them ahead of time

declare module '*.module.css' {
const classes: {readonly [key: string]: string};
export default classes;
Expand Down
25 changes: 5 additions & 20 deletions website/docs/typescript-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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" />
```

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

{
  "extends": "@tsconfig/docusaurus",
  "types": ["node",  "jest", "@docusaurus/module-type-aliases"],
  "include": ["src"]
}

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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config specifies noEmit - so no need to include it here


Now you can start writing TypeScript theme components.

Expand Down