better types

This commit is contained in:
kakkokari-gtyih 2024-06-02 19:40:36 +09:00
parent 2968e1be4b
commit 774b46be94
1 changed files with 12 additions and 4 deletions

View File

@ -11,10 +11,18 @@ export const postMessageEventTypes = [
export type PostMessageEventType = typeof postMessageEventTypes[number]; export type PostMessageEventType = typeof postMessageEventTypes[number];
export type MiPostMessageEvent = { export interface PostMessageEventPayload extends Record<PostMessageEventType, any> {
type: PostMessageEventType; 'misskey:shareForm:shareCompleted': undefined;
'misskey:embed:ready': undefined;
'misskey:embed:changeHeight': {
height: number;
};
};
export type MiPostMessageEvent<T extends PostMessageEventType = PostMessageEventType> = {
type: T;
iframeId?: string; iframeId?: string;
payload?: any; payload?: PostMessageEventPayload[T];
}; };
let defaultIframeId: string | null = null; let defaultIframeId: string | null = null;
@ -27,7 +35,7 @@ export function setIframeId(id: string): void {
/** /**
* *
*/ */
export function postMessageToParentWindow(type: PostMessageEventType, payload?: any, iframeId: string | null = null): void { export function postMessageToParentWindow<T extends PostMessageEventType = PostMessageEventType>(type: T, payload?: PostMessageEventPayload[T], iframeId: string | null = null): void {
let _iframeId = iframeId; let _iframeId = iframeId;
if (_iframeId == null) { if (_iframeId == null) {
_iframeId = defaultIframeId; _iframeId = defaultIframeId;