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

refactor: Create a more prominent TS-subsection in guide #990

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
92 changes: 92 additions & 0 deletions content/en/guide/v10/configuring-typescript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
name: Configuring TypeScript
description: "Preact has built-in TypeScript support. Learn how to configure it!"
---

# TypeScript

Preact ships TypeScript type definitions, which are used by the library itself!

When you use Preact in a TypeScript-aware editor (like VSCode), you can benefit from the added type information even while writing regular JavaScript. If you want to add type information to your own applications, you can use [JSDoc annotations](https://fettblog.eu/typescript-jsdoc-superpowers/), or write TypeScript and transpile to regular JavaScript. These docs will focus on the latter.

---

<div><toc></toc></div>

---

## Configuring TypeScript

TypeScript includes a full-fledged JSX compiler that you can use instead of Babel. Add the following configuration to your `tsconfig.json` to transpile JSX to Preact-compatible JavaScript:

```json
// TypeScript >= 4.1.1
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact",
//...
}
}
```
```json
// TypeScript < 4.1.1
{
"compilerOptions": {
"jsx": "react",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
//...
}
}
```

If you use TypeScript within a Babel toolchain, set `jsx` to `preserve` and let Babel handle the transpilation. You still need to specify `jsxFactory` and `jsxFragmentFactory` to get the correct types.

```json
{
"compilerOptions": {
"jsx": "preserve",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
//...
}
}
```

In your `.babelrc`:

```javascript
{
presets: [
"@babel/env",
["@babel/typescript", { jsxPragma: "h" }],
],
plugins: [
["@babel/transform-react-jsx", { pragma: "h" }]
],
}
```

Rename your `.jsx` files to `.tsx` for TypeScript to correctly parse your JSX.

## Configuring with `preact/compat`

If you're using `preact/compat` in your application to get access to the wider React ecosystem,
you may need to disable type checking declaration files (`.d.ts`) and/or add path aliases to ensure
your React-based libraries are consuming Preact's types instead:

```json
{
"compilerOptions": {
...
"skipLibCheck": true,
...
"baseUrl": "./",
"paths": {
"react": ["./node_modules/preact/compat/"],
"react-dom": ["./node_modules/preact/compat/"]
}
}
}
```
Original file line number Diff line number Diff line change
@@ -1,95 +1,20 @@
---
name: TypeScript
name: Using TypeScript
description: "Preact has built-in TypeScript support. Learn how to make use of it!"
---

# TypeScript

Preact ships TypeScript type definitions, which are used by the library itself!
Preact ships TypeScript type definitions, which are used by the library itself!

When you use Preact in a TypeScript-aware editor (like VSCode), you can benefit from the added type information while writing regular JavaScript. If you want to add type information to your own applications, you can use [JSDoc annotations](https://fettblog.eu/typescript-jsdoc-superpowers/), or write TypeScript and transpile to regular JavaScript. This section will focus on the latter.
When you use Preact in a TypeScript-aware editor (like VSCode), you can benefit from the added type information even while writing regular JavaScript. If you want to add type information to your own applications, you can use [JSDoc annotations](https://fettblog.eu/typescript-jsdoc-superpowers/), or write TypeScript and transpile to regular JavaScript. These docs will focus on the latter.

---

<div><toc></toc></div>

---

## TypeScript configuration

TypeScript includes a full-fledged JSX compiler that you can use instead of Babel. Add the following configuration to your `tsconfig.json` to transpile JSX to Preact-compatible JavaScript:

```json
// TypeScript < 4.1.1
{
"compilerOptions": {
"jsx": "react",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
//...
}
}
```
```json
// TypeScript >= 4.1.1
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact",
//...
}
}
```

If you use TypeScript within a Babel toolchain, set `jsx` to `preserve` and let Babel handle the transpilation. You still need to specify `jsxFactory` and `jsxFragmentFactory` to get the correct types.

```json
{
"compilerOptions": {
"jsx": "preserve",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
//...
}
}
```

In your `.babelrc`:

```javascript
{
presets: [
"@babel/env",
["@babel/typescript", { jsxPragma: "h" }],
],
plugins: [
["@babel/transform-react-jsx", { pragma: "h" }]
],
}
```

Rename your `.jsx` files to `.tsx` for TypeScript to correctly parse your JSX.

## TypeScript preact/compat configuration

Your project could need support for the wider React ecosystem. To make your application
compile, you might need to disable type checking on your `node_modules` and add paths to the types
like this. This way, your alias will work properly when libraries import React.

```json
{
"compilerOptions": {
...
"skipLibCheck": true,
"baseUrl": "./",
"paths": {
"react": ["./node_modules/preact/compat/"],
"react-dom": ["./node_modules/preact/compat/"]
}
}
}
```

## Typing components

There are different ways to type components in Preact. Class components have generic type variables to ensure type safety. TypeScript sees a function as functional component as long as it returns JSX. There are multiple solutions to define props for functional components.
Expand Down
83 changes: 49 additions & 34 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,55 @@
}
]
},
{
"name": {
"en": "React compatibility",
"zh": "React 兼容性"
},
Comment on lines +385 to +389
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved this section up as compatibility, for better or worse, is a major part of our ecosystem and draw. Most users are probably going to want to read that right after intro + essentials, debugging can get bumped down a slot.

"routes": [
{
"path": "/guide/v10/differences-to-react",
"name": {
"en": "Differences to React",
"pt-br": "Diferenças do React",
"ja": "Reactとの違い",
"de": "Unterschiede zu React",
"kr": "React와의 차이점",
"zh": "与 React 的区别"
}
},
{
"path": "/guide/v10/switching-to-preact",
"name": {
"en": "Switching to Preact",
"pt-br": "Mudando para Preact",
"ja": "Preactへの移行",
"de": "Wechsel zu Preact",
"kr": "Preact로 전환",
"zh": "从 React 转到 Preact"
}
}
]
},
{
"name": {
"en": "TypeScript"
},
"routes": [
{
"path": "/guide/v10/configuring-typescript",
"name": {
"en": "Configuring TypeScript"
}
},
{
"path": "/guide/v10/using-typescript",
"name": {
"en": "Using TypeScript"
}
}
]
},
{
"name": {
"en": "Debug & Test",
Expand Down Expand Up @@ -421,36 +470,6 @@
}
]
},
{
"name": {
"en": "React compatibility",
"zh": "React 兼容性"
},
"routes": [
{
"path": "/guide/v10/differences-to-react",
"name": {
"en": "Differences to React",
"pt-br": "Diferenças do React",
"ja": "Reactとの違い",
"de": "Unterschiede zu React",
"kr": "React와의 차이점",
"zh": "与 React 的区别"
}
},
{
"path": "/guide/v10/switching-to-preact",
"name": {
"en": "Switching to Preact",
"pt-br": "Mudando para Preact",
"ja": "Preactへの移行",
"de": "Wechsel zu Preact",
"kr": "Preact로 전환",
"zh": "从 React 转到 Preact"
}
}
]
},
{
"name": {
"en": "Advanced",
Expand Down Expand Up @@ -513,10 +532,6 @@
"de": "Optionen",
"zh": "选项钩子"
}
},
{
"path": "/guide/v10/typescript",
"name": "TypeScript"
}
]
},
Expand Down