From dbb35d102f748d9abc436fd98980be0da86efc8e Mon Sep 17 00:00:00 2001 From: jonny Date: Fri, 28 May 2021 01:59:43 +0200 Subject: [PATCH] extracted giant typing block --- front/src/iframe_api.d.ts | 25 +++++++++++++++++++++++++ front/src/iframe_api.ts | 33 ++------------------------------- 2 files changed, 27 insertions(+), 31 deletions(-) create mode 100644 front/src/iframe_api.d.ts diff --git a/front/src/iframe_api.d.ts b/front/src/iframe_api.d.ts new file mode 100644 index 00000000..953c92d2 --- /dev/null +++ b/front/src/iframe_api.d.ts @@ -0,0 +1,25 @@ +import type { WorkadventureImport } from './iframe_api'; + +type PromiseReturnType

= P extends Promise ? T : P; +type WorkadventureCommandClasses = PromiseReturnType[number]["default"]; +type KeysOfUnion = T extends T ? keyof T : never; +type ObjectWithKeyOfUnion = O extends O ? (Key extends keyof O ? O[Key] : never) : never; +type ApiKeys = KeysOfUnion; +type ObjectOfKey = O extends O ? (Key extends keyof O ? O : never) : never; +type ShouldAddAttribute = ObjectWithKeyOfUnion; +type WorkadventureFunctions = { + [K in ApiKeys]: ObjectWithKeyOfUnion extends Function ? K : never; +}[ApiKeys]; +type WorkadventureFunctionsFilteredByRoot = { + [K in WorkadventureFunctions]: ObjectOfKey["addMethodsAtRoot"] extends true ? K : never; +}[WorkadventureFunctions]; +type JustMethodKeys = ({ + [P in keyof T]: T[P] extends Function ? P : never; +})[keyof T]; +type JustMethods = Pick>; +type SubObjectTypes = { + [importCl in WorkadventureCommandClasses as importCl["subObjectIdentifier"]]: JustMethods; +}; +export type WorkAdventureApi = { + [Key in WorkadventureFunctionsFilteredByRoot]: ShouldAddAttribute; +} & SubObjectTypes; diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index 0da0099f..136edb8e 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -1,4 +1,5 @@ import { IframeResponseEventMap, isIframeResponseEventWrapper } from "./Api/Events/IframeEvent"; +import type { WorkAdventureApi } from './iframe_api.d'; export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: { typeChecker: Function @@ -16,37 +17,7 @@ const importType = Promise.all([ import("./Api/iframe/Bubble") ]) - -type PromiseReturnType

= P extends Promise ? T : P - -type WorkadventureCommandClasses = PromiseReturnType[number]["default"]; - -type KeysOfUnion = T extends T ? keyof T : never - -type ObjectWithKeyOfUnion = O extends O ? (Key extends keyof O ? O[Key] : never) : never - -type ApiKeys = KeysOfUnion; - -type ObjectOfKey = O extends O ? (Key extends keyof O ? O : never) : never - -type ShouldAddAttribute = ObjectWithKeyOfUnion; - -type WorkadventureFunctions = { [K in ApiKeys]: ObjectWithKeyOfUnion extends Function ? K : never }[ApiKeys] - -type WorkadventureFunctionsFilteredByRoot = { [K in WorkadventureFunctions]: ObjectOfKey["addMethodsAtRoot"] extends true ? K : never }[WorkadventureFunctions] - - -type JustMethodKeys = ({ [P in keyof T]: T[P] extends Function ? P : never })[keyof T]; -type JustMethods = Pick>; - -type SubObjectTypes = { - [importCl in WorkadventureCommandClasses as importCl["subObjectIdentifier"]]: JustMethods; -}; - -type WorkAdventureApi = { - [Key in WorkadventureFunctionsFilteredByRoot]: ShouldAddAttribute -} & SubObjectTypes - +export type WorkadventureImport = typeof importType declare global {