js事件对象e的ts类型校验
时间: 2023-10-27 18:00:56 浏览: 89
可以使用以下类型来校验JS事件对象e的类型:
```typescript
interface MouseEvent extends UIEvent {
readonly altKey: boolean;
readonly button: number;
readonly buttons: number;
readonly clientX: number;
readonly clientY: number;
readonly ctrlKey: boolean;
readonly metaKey: boolean;
readonly movementX: number;
readonly movementY: number;
readonly offsetX: number;
readonly offsetY: number;
readonly pageX: number;
readonly pageY: number;
readonly relatedTarget: EventTarget | null;
readonly screenX: number;
readonly screenY: number;
readonly shiftKey: boolean;
readonly x: number;
readonly y: number;
}
interface KeyboardEvent extends UIEvent {
readonly altKey: boolean;
readonly code: string;
readonly ctrlKey: boolean;
readonly isComposing: boolean;
readonly key: string;
readonly location: number;
readonly metaKey: boolean;
readonly repeat: boolean;
readonly shiftKey: boolean;
}
interface UIEvent extends Event {
readonly detail: number;
readonly view: Window | null;
}
interface Event {
readonly bubbles: boolean;
cancelBubble: boolean;
readonly cancelable: boolean;
readonly composed: boolean;
readonly currentTarget: EventTarget | null;
readonly defaultPrevented: boolean;
readonly eventPhase: number;
readonly isTrusted: boolean;
returnValue: boolean;
readonly target: EventTarget | null;
readonly timeStamp: number;
readonly type: string;
composedPath(): EventTarget[];
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;
preventDefault(): void;
stopImmediatePropagation(): void;
stopPropagation(): void;
}
interface EventTarget {
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
dispatchEvent(event: Event): boolean;
removeEventListener(type: string, listener?: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
}
```
以上这些类型都是内置的TypeScript类型,可以直接使用。
阅读全文