fix: add LiteGraph types to Window interface and remove @ts-expect-error suppressions

This commit is contained in:
DrJKL
2026-01-11 12:21:38 -08:00
parent b674b7397e
commit 3ca02ad67c
3 changed files with 65 additions and 28 deletions

View File

@@ -1,14 +1,38 @@
import * as fs from 'fs' import * as fs from 'fs'
import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' import type { ComfyNodeDef } from '@/schemas/nodeDefSchema'
import type { ComfyApp } from '@/scripts/app'
import type { LiteGraphGlobal } from '../src/lib/litegraph/src/litegraph'
import { comfyPageFixture as test } from '../browser_tests/fixtures/ComfyPage' import { comfyPageFixture as test } from '../browser_tests/fixtures/ComfyPage'
import { normalizeI18nKey } from '../packages/shared-frontend-utils/src/formatUtil' import { normalizeI18nKey } from '@comfyorg/shared-frontend-utils/formatUtil'
import type { ComfyNodeDefImpl } from '../src/stores/nodeDefStore' import type { ComfyNodeDefImpl } from '../src/stores/nodeDefStore'
const localePath = './src/locales/en/main.json' const localePath = './src/locales/en/main.json'
const nodeDefsPath = './src/locales/en/nodeDefs.json' const nodeDefsPath = './src/locales/en/nodeDefs.json'
interface ComfyWindow extends Window {
app: ComfyApp
LiteGraph: LiteGraphGlobal
}
function isComfyWindow(win: Window): win is ComfyWindow {
return (
'app' in win &&
win.app != null &&
'LiteGraph' in win &&
win.LiteGraph != null
)
}
function getComfyWindow(): ComfyWindow {
if (!isComfyWindow(window)) {
throw new Error('ComfyApp or LiteGraph not found on window')
}
return window
}
interface WidgetInfo { interface WidgetInfo {
name?: string name?: string
label?: string label?: string
@@ -30,8 +54,8 @@ test('collect-i18n-node-defs', async ({ comfyPage }) => {
const nodeDefs: ComfyNodeDefImpl[] = await comfyPage.page.evaluate( const nodeDefs: ComfyNodeDefImpl[] = await comfyPage.page.evaluate(
async () => { async () => {
// @ts-expect-error - app is dynamically added to window const comfyWindow = getComfyWindow()
const api = window['app'].api const api = comfyWindow.app.api
const rawNodeDefs = await api.getNodeDefs() const rawNodeDefs = await api.getNodeDefs()
const { ComfyNodeDefImpl } = await import('../src/stores/nodeDefStore') const { ComfyNodeDefImpl } = await import('../src/stores/nodeDefStore')
@@ -44,7 +68,7 @@ test('collect-i18n-node-defs', async ({ comfyPage }) => {
} }
) )
console.log(`Collected ${nodeDefs.length} node definitions`) console.warn(`Collected ${nodeDefs.length} node definitions`)
const allDataTypesLocale = Object.fromEntries( const allDataTypesLocale = Object.fromEntries(
nodeDefs nodeDefs
@@ -80,9 +104,9 @@ test('collect-i18n-node-defs', async ({ comfyPage }) => {
const widgetsMappings = await comfyPage.page.evaluate( const widgetsMappings = await comfyPage.page.evaluate(
(args) => { (args) => {
const [nodeName, displayName, inputNames] = args const [nodeName, displayName, inputNames] = args
// @ts-expect-error - LiteGraph is dynamically added to window const comfyWindow = getComfyWindow()
const node = window['LiteGraph'].createNode(nodeName, displayName) const node = comfyWindow.LiteGraph.createNode(nodeName, displayName)
if (!node.widgets?.length) return {} if (!node?.widgets?.length) return {}
return Object.fromEntries( return Object.fromEntries(
node.widgets node.widgets
.filter( .filter(

View File

@@ -6,30 +6,21 @@ import {
LGraphCanvas, LGraphCanvas,
LGraphGroup, LGraphGroup,
LGraphNode, LGraphNode,
LLink, LiteGraph,
LiteGraph LLink
} from '@/lib/litegraph/src/litegraph' } from '@/lib/litegraph/src/litegraph'
/** /**
* Assign all properties of LiteGraph to window to make it backward compatible. * Assign all properties of LiteGraph to window to make it backward compatible.
*/ */
export const useGlobalLitegraph = () => { export function useGlobalLitegraph() {
// @ts-expect-error fixme ts strict error window.LiteGraph = LiteGraph
window['LiteGraph'] = LiteGraph window.LGraph = LGraph
// @ts-expect-error fixme ts strict error window.LLink = LLink
window['LGraph'] = LGraph window.LGraphNode = LGraphNode
// @ts-expect-error fixme ts strict error window.LGraphGroup = LGraphGroup
window['LLink'] = LLink window.DragAndScale = DragAndScale
// @ts-expect-error fixme ts strict error window.LGraphCanvas = LGraphCanvas
window['LGraphNode'] = LGraphNode window.ContextMenu = ContextMenu
// @ts-expect-error fixme ts strict error window.LGraphBadge = LGraphBadge
window['LGraphGroup'] = LGraphGroup
// @ts-expect-error fixme ts strict error
window['DragAndScale'] = DragAndScale
// @ts-expect-error fixme ts strict error
window['LGraphCanvas'] = LGraphCanvas
// @ts-expect-error fixme ts strict error
window['ContextMenu'] = ContextMenu
// @ts-expect-error fixme ts strict error
window['LGraphBadge'] = LGraphBadge
} }

View File

@@ -1,3 +1,14 @@
import type {
ContextMenu,
DragAndScale,
LGraph,
LGraphBadge,
LGraphCanvas,
LGraphGroup,
LGraphNode,
LiteGraphGlobal,
LLink
} from '@/lib/litegraph/src/litegraph'
import type { import type {
DeviceStats, DeviceStats,
EmbeddingsResponse, EmbeddingsResponse,
@@ -71,5 +82,16 @@ declare global {
/** For use by extensions and in the browser console. Where possible, import `app` and access via `app.graph` instead. */ /** For use by extensions and in the browser console. Where possible, import `app` and access via `app.graph` instead. */
graph?: unknown graph?: unknown
/** LiteGraph global namespace - for extension compatibility */
LiteGraph?: LiteGraphGlobal
LGraph?: typeof LGraph
LLink?: typeof LLink
LGraphNode?: typeof LGraphNode
LGraphGroup?: typeof LGraphGroup
DragAndScale?: typeof DragAndScale
LGraphCanvas?: typeof LGraphCanvas
ContextMenu?: typeof ContextMenu
LGraphBadge?: typeof LGraphBadge
} }
} }