[Typescript] Extracting Members of a Discriminated union - Extract<T, U>
typescript of gt lt union extract
2023-09-14 08:59:11 时间
Give a discriminated union:
export type Event =
| {
type: "click";
event: MouseEvent;
}
| {
type: "focus";
event: FocusEvent;
}
| {
type: "keydown";
event: KeyboardEvent;
};
I want to get only Click event, we can reply on Extract
type utilites.
type ClickEvent = Extract<Event, { type: "click" }>;
import { Equal, Expect } from "../helpers/type-utils";
export type Event =
| {
type: "click";
event: MouseEvent;
}
| {
type: "focus";
event: FocusEvent;
}
| {
type: "keydown";
event: KeyboardEvent;
};
type ClickEvent = Extract<Event, { type: "click" }>;
type tests = [Expect<Equal<ClickEvent, { type: "click"; event: MouseEvent }>>];
相关文章
- [Typescript] Defining exclusive properties with TypeScript
- [Typescript Challenges] 5. Easy - Length of Tuple
- [Typescript] Convert a js project to typescript
- [Typescript] Generics constraint
- [TypeScript] infer
- [Typescript] Simplify iteration of custom data structures in TypeScript with iterators (backwards iteration with for ... of.. loop)
- [Typescript] Specify Exact Values with TypeScript’s Literal Types
- [Typescript] Builder pattern - 02
- [Typescript] 73. Medium - Join
- [TypeScript] Make typescript stick - 2
- [TypeScript] Class Property Inference from Constructors V4
- [Typescript] “Required” Mapped Type, +/- Modifiers
- [React Native] Up & Running with React Native & TypeScript
- [Typescript Kaop-ts] Use AOP in Vue Components with TypeScript and Kaop-ts
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
- 带你了解Typescript的14个基础语法
- 实现一个自动生成typescript类型声明的工具