Skip to content

Instantly share code, notes, and snippets.

@Vendicated
Last active August 16, 2024 13:47
Show Gist options
  • Save Vendicated/472fa7a3fd92a52bfe2354c2e5e7c4e7 to your computer and use it in GitHub Desktop.
Save Vendicated/472fa7a3fd92a52bfe2354c2e5e7c4e7 to your computer and use it in GitHub Desktop.
Generate a typescript Interface from any object (does not support circular references)
var identifierRegex = /^[A-Za-z_$]\w*$/;
function getType(v, indent) {
switch (typeof v) {
case "bigint":
case "boolean":
case "number":
case "string":
case "symbol":
return typeof v + ";";
case "function":
if (v.length) {
if (v.length > 10) return "(...args: any[]) => any;";
return `(${Array.from({ length: v.length }, (_, i) => `p${i}: any`).join(", ")}) => any;`;
}
const str = v.toString();
const mightHaveVarArgs =
str === "function () { [native code] }"
|| str.includes("arguments")
|| /\(.+?\.{3}.+?\)/.test(str);
if (mightHaveVarArgs) return "(...args: any[]) => any;";
return "() => any;";
case "object":
if (v === null) return "any | null";
return genInterface(v, true, indent + " ");
case "undefined":
return "any | undefined;";
default:
throw new Error("??");
}
}
function genInterface(obj, isInner = false, indent = " ") {
let inter = `${isInner ? "" : "interface RootObject "}{\n`;
for (const key in obj) {
const sanitizedKey = identifierRegex.test(key) ? key : JSON.stringify(key);
inter += `${indent}${sanitizedKey}: ${getType(obj[key], indent)}\n`;
}
return `${inter}${indent.slice(4)}};`;
}
interface RootObject {
isRenderer: boolean;
setUncaughtExceptionHandler: (...args: any[]) => any;
nativeModules: {
ensureModule: (...args: any[]) => any;
requireModule: (...args: any[]) => any;
canBootstrapNewUpdater: boolean;
};
process: {
platform: string;
arch: string;
env: {
DISCORD_TEST: any | undefined;
DISCORD_GATEWAY_PLAINTEXT: any | undefined;
DISCORD_DISALLOW_POPUPS: any | undefined;
LOCALAPPDATA: string;
"PROGRAMFILES(X86)": string;
PROGRAMFILES: string;
PROGRAMW6432: string;
PROGRAMDATA: string;
};
};
os: {
release: string;
arch: string;
};
app: {
getReleaseChannel: (...args: any[]) => any;
getVersion: (...args: any[]) => any;
getModuleVersions: (...args: any[]) => any;
getBuildNumber: (...args: any[]) => any;
getPath: (...args: any[]) => any;
setBadgeCount: (...args: any[]) => any;
dock: {
setBadge: (...args: any[]) => any;
bounce: (...args: any[]) => any;
cancelBounce: (...args: any[]) => any;
};
relaunch: (...args: any[]) => any;
getDefaultDoubleClickAction: (...args: any[]) => any;
registerUserInteractionHandler: (...args: any[]) => any;
};
clipboard: {
copy: (...args: any[]) => any;
copyImage: (...args: any[]) => any;
cut: (...args: any[]) => any;
paste: (...args: any[]) => any;
read: (...args: any[]) => any;
};
ipc: {
send: (...args: any[]) => any;
on: (...args: any[]) => any;
invoke: (...args: any[]) => any;
};
gpuSettings: {
getEnableHardwareAcceleration: (...args: any[]) => any;
setEnableHardwareAcceleration: (...args: any[]) => any;
};
window: {
flashFrame: (...args: any[]) => any;
minimize: (...args: any[]) => any;
restore: (...args: any[]) => any;
maximize: (...args: any[]) => any;
focus: (...args: any[]) => any;
blur: (...args: any[]) => any;
fullscreen: (...args: any[]) => any;
close: (...args: any[]) => any;
setAlwaysOnTop: (...args: any[]) => any;
isAlwaysOnTop: (...args: any[]) => any;
setZoomFactor: (...args: any[]) => any;
setBackgroundThrottling: (...args: any[]) => any;
setProgressBar: (...args: any[]) => any;
setDevtoolsCallbacks: (...args: any[]) => any;
USE_OSX_NATIVE_TRAFFIC_LIGHTS: boolean;
};
powerMonitor: {
on: (...args: any[]) => any;
removeListener: (...args: any[]) => any;
removeAllListeners: (...args: any[]) => any;
getSystemIdleTimeMs: (...args: any[]) => any;
};
spellCheck: {
on: (...args: any[]) => any;
removeListener: (...args: any[]) => any;
getAvailableDictionaries: (...args: any[]) => any;
setLocale: (...args: any[]) => any;
setLearnedWords: (...args: any[]) => any;
replaceMisspelling: (...args: any[]) => any;
};
crashReporter: {
updateCrashReporter: (...args: any[]) => any;
getMetadata: (...args: any[]) => any;
};
desktopCapture: {
getDesktopCaptureSources: (...args: any[]) => any;
};
fileManager: {
readLogFiles: (...args: any[]) => any;
readTimeSeriesLogFiles: (...args: any[]) => any;
cleanupTempFiles: (...args: any[]) => any;
saveWithDialog: (...args: any[]) => any;
openFiles: (...args: any[]) => any;
showOpenDialog: (...args: any[]) => any;
showItemInFolder: (...args: any[]) => any;
getModulePath: (...args: any[]) => any;
getModuleDataPathSync: (...args: any[]) => any;
getCrashFiles: (...args: any[]) => any;
extname: (...args: any[]) => any;
basename: (...args: any[]) => any;
dirname: (...args: any[]) => any;
join: (...args: any[]) => any;
};
processUtils: {
flushDNSCache: (...args: any[]) => any;
flushCookies: (...args: any[]) => any;
getLastCrash: (...args: any[]) => any;
flushStorageData: (...args: any[]) => any;
purgeMemory: (...args: any[]) => any;
getCurrentCPUUsagePercent: (...args: any[]) => any;
getCurrentMemoryUsageKB: (...args: any[]) => any;
getCPUCoreCount: (...args: any[]) => any;
getMainArgvSync: (...args: any[]) => any;
};
powerSaveBlocker: {
blockDisplaySleep: (...args: any[]) => any;
unblockDisplaySleep: (...args: any[]) => any;
cleanupDisplaySleep: (...args: any[]) => any;
};
http: {
getAPIEndpoint: (...args: any[]) => any;
makeChunkedRequest: (...args: any[]) => any;
};
accessibility: {
isAccessibilitySupportEnabled: (...args: any[]) => any;
};
features: {
supports: (...args: any[]) => any;
declareSupported: (...args: any[]) => any;
};
settings: {
get: (...args: any[]) => any;
set: (...args: any[]) => any;
getSync: (...args: any[]) => any;
};
userDataCache: {
getCached: (...args: any[]) => any;
cacheUserData: (...args: any[]) => any;
deleteCache: (...args: any[]) => any;
};
thumbar: {
setThumbarButtons: (...args: any[]) => any;
};
safeStorage: {
isEncryptionAvailable: (...args: any[]) => any;
decryptString: (...args: any[]) => any;
encryptString: (...args: any[]) => any;
};
remoteApp: {
getReleaseChannel: (...args: any[]) => any;
getVersion: (...args: any[]) => any;
getModuleVersions: (...args: any[]) => any;
getBuildNumber: (...args: any[]) => any;
getPath: (...args: any[]) => any;
setBadgeCount: (...args: any[]) => any;
dock: {
setBadge: (...args: any[]) => any;
bounce: (...args: any[]) => any;
cancelBounce: (...args: any[]) => any;
};
relaunch: (...args: any[]) => any;
getDefaultDoubleClickAction: (...args: any[]) => any;
registerUserInteractionHandler: (...args: any[]) => any;
};
remotePowerMonitor: {
on: (...args: any[]) => any;
removeListener: (...args: any[]) => any;
removeAllListeners: (...args: any[]) => any;
getSystemIdleTimeMs: (...args: any[]) => any;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment