You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Even though we have narrowed based on `event.value`, the logic doesn't filter up and sideways to `event.target`. This is because a union type `UserTextEvent | UserMouseEvent` could be BOTH at once. So TypeScript needs a better hint. The solution is to use a literal type to tag each case of your union type:
530
-
531
514
```ts
532
515
typeUserTextEvent= {
533
516
type:"TextEvent";
@@ -551,6 +534,30 @@ function handle(event: UserEvent) {
551
534
}
552
535
```
553
536
537
+
<details>
538
+
<summary>
539
+
Take care: TypeScript does not narrow the type of a Discriminated Union on the basis of typeof checks. The type guard has to be on the value of a key and not it's type.
The above example does not work as we are not checking the value of `event.value` but only it's type. Read more about it [microsoft/TypeScript#30506 (comment)](https://github.com/microsoft/TypeScript/issues/30506#issuecomment-474858198)
558
+
559
+
</details>
560
+
554
561
To streamline this you may also combine this with the concept of **User-Defined Type Guards**:
0 commit comments