Currently I have the following code:
declare const isBrowser: boolean;
declare const API: BrowserAPI & ElectronAPI;
allowing me to do something such as the following:
if (isBrowser) API.browserOnlyFunction();
which works great, but unfortunately because of the union, this makes it so I can use API.browserOnlyFunction anywhere.
What I want to do is the following:
API; // BrowserAPI | ElectronAPI
if (isBrowser) {
API; // BrowserAPI
} else {
API; // ElectronAPI
}
I've tried doing the following but with no avail:
declare const isBrowser: boolean;
declare const API: typeof isBrowser extends true ? BrowserAPI : ElectronAPI; // is always ElectronAPI
// having to pass API as an argument every time seems pretty ugly and repetitive to me
function browserAPI(API: BrowserAPI | ElectronAPI): API is BrowserAPI {
if (isBrowser) return true;
return false;
}
Is there any way I can do this with just the isBrowser constant or just a function that doesn't take any arguments?