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

docs: Switch TS Events docs to use .currentTarget #1241

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
31 changes: 14 additions & 17 deletions content/en/guide/v10/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: "Preact has built-in TypeScript support. Learn how to make use of i

# 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.

Expand Down Expand Up @@ -233,35 +233,32 @@ Now when we use `Input` it will know about properties like `value`, ...
Preact emits regular DOM events. As long as your TypeScript project includes the `dom` library (set it in `tsconfig.json`), you have access to all event types that are available in your current configuration.

```tsx
import type { JSX } from "preact";

export class Button extends Component {
handleClick(event: MouseEvent) {
event.preventDefault();
if (event.target instanceof HTMLElement) {
alert(event.target.tagName); // Alerts BUTTON
}
handleClick(event: JSX.TargetedMouseEvent<HTMLButtonElement>) {
alert(event.currentTarget.tagName); // Alerts BUTTON
}

render() {
return <button onClick={this.handleClick}>{this.props.children}</button>;
return (
<button onClick={this.handleClick}>
{this.props.children}
</button>
);
}
}
```

You can restrict event handlers by adding a type annotation for `this` to the function signature as the first argument. This argument will be erased after transpilation.
If you prefer inline functions, you can forgo explicitly typing the current event target as it is inferred from the JSX element:

```tsx
export class Button extends Component {
// Adding the this argument restricts binding
handleClick(this: HTMLButtonElement, event: MouseEvent) {
event.preventDefault();
if (event.target instanceof HTMLElement) {
console.log(event.target.localName); // "button"
}
}

render() {
return (
<button onClick={this.handleClick}>{this.props.children}</button>
<button onClick={(event) => alert(event.currentTarget.tagName)}>
{this.props.children}
</button>
);
}
}
Expand Down