Skip to content

Commit 33208b4

Browse files
authored
Show inline event type inference in the TypeScript guide
1 parent 046f17d commit 33208b4

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎src/content/learn/typescript.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,16 @@ There is quite an expansive set of types which come from the `@types/react` pack
389389

390390
### DOM Events {/*typing-dom-events*/}
391391

392-
When working with DOM events in React, the type of the event can often be inferred from the event handler. However, when you want to extract a function to be passed to an event handler, you will need to explicitly set the type of the event.
392+
When working with DOM events in React, TypeScript can infer the event type when you write the event handler inline. For example, the `event` parameter below is inferred as `React.ChangeEvent<HTMLInputElement>`:
393+
394+
```tsx
395+
<input
396+
value={value}
397+
onChange={event => setValue(event.currentTarget.value)}
398+
/>
399+
```
400+
401+
However, when you want to extract a function to be passed to an event handler, you will need to explicitly set the type of the event:
393402

394403
<Sandpack>
395404

0 commit comments

Comments
 (0)