-
-
Notifications
You must be signed in to change notification settings - Fork 510
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
rschristian
wants to merge
2
commits into
master
Choose a base branch
from
feat/typescript-section
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"] | ||
} | ||
} | ||
} | ||
``` |
81 changes: 3 additions & 78 deletions
81
content/en/guide/v10/typescript.md → content/en/guide/v10/using-typescript.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.