diff --git a/src/components/App.tsx b/src/components/App.tsx index 171d71b2..e6b39983 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -301,9 +301,9 @@ class App extends React.Component { }; if (excalidrawRef) { const readyPromise = - typeof excalidrawRef === "function" - ? resolvablePromise() - : excalidrawRef.current!.readyPromise; + ("current" in excalidrawRef && excalidrawRef.current?.readyPromise) || + resolvablePromise(); + const api: ExcalidrawImperativeAPI = { ready: true, readyPromise, diff --git a/src/excalidraw-app/index.tsx b/src/excalidraw-app/index.tsx index 07768a46..2fcd0b92 100644 --- a/src/excalidraw-app/index.tsx +++ b/src/excalidraw-app/index.tsx @@ -24,7 +24,9 @@ import { ExcalidrawElement } from "../element/types"; import { SAVE_TO_LOCAL_STORAGE_TIMEOUT } from "./app_constants"; import { EVENT_LOAD, EVENT_SHARE, trackEvent } from "../analytics"; -const excalidrawRef: React.MutableRefObject = { +const excalidrawRef: React.MutableRefObject< + MarkRequired +> = { current: { readyPromise: resolvablePromise(), ready: false, diff --git a/src/global.d.ts b/src/global.d.ts index 25a7d1fc..8bce07a7 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -43,6 +43,9 @@ type ResolutionType any> = T extends ( // https://github.com/krzkaczor/ts-essentials type MarkOptional = Omit & Partial>; +type MarkRequired = Exclude & + Required>; + // PNG encoding/decoding // ----------------------------------------------------------------------------- type TEXtChunk = { name: "tEXt"; data: Uint8Array }; diff --git a/src/types.ts b/src/types.ts index 161b7263..5106b656 100644 --- a/src/types.ts +++ b/src/types.ts @@ -133,14 +133,16 @@ export declare class GestureEvent extends UIEvent { export type LibraryItem = readonly NonDeleted[]; export type LibraryItems = readonly LibraryItem[]; +// NOTE ready/readyPromise props are optional for host apps' sake (our own +// implem guarantees existence) export type ExcalidrawAPIRefValue = | (ExcalidrawImperativeAPI & { - readyPromise: ResolvablePromise; - ready: true; + readyPromise?: ResolvablePromise; + ready?: true; }) | { - readyPromise: ResolvablePromise; - ready: false; + readyPromise?: ResolvablePromise; + ready?: false; }; export interface ExcalidrawProps {