fix: replace as-unknown-as casts with safer patterns (#9107)

## Summary

- Replace 83 `as unknown as` double casts with safer alternatives across
33 files
- Use `as Partial<X> as X` pattern where TypeScript allows it
- Create/reuse factory functions from `litegraphTestUtils.ts` for mock
objects
- Widen `getWorkflowDataFromFile` return type to include `ComfyMetadata`
directly
- Reduce total `as unknown as` count from ~153 to 71

The remaining 71 occurrences are genuinely necessary due to cross-schema
casts, generic variance, missing index signatures, Float64Array-to-tuple
conversions, and DOM type incompatibilities.

## Test plan

- [x] `pnpm typecheck` passes
- [x] `pnpm lint` passes
- [x] All affected unit tests pass

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-9107-fix-replace-as-unknown-as-casts-with-safer-patterns-3106d73d3650815cb5bcd613ad635bd7)
by [Unito](https://www.unito.io)
This commit is contained in:
Johnpaul Chiwetelu
2026-02-23 05:46:12 +01:00
committed by GitHub
parent 8998d92e1b
commit 02e926471f
33 changed files with 282 additions and 346 deletions

View File

@@ -5,6 +5,7 @@ import { getDataFromJSON } from '@/scripts/metadata/json'
import { getMp3Metadata } from '@/scripts/metadata/mp3'
import { getOggMetadata } from '@/scripts/metadata/ogg'
import { getSvgMetadata } from '@/scripts/metadata/svg'
import type { ComfyMetadata } from '@/types/metadataTypes'
import {
getAvifMetadata,
getWebpMetadata,
@@ -15,7 +16,7 @@ import {
export async function getWorkflowDataFromFile(
file: File
): Promise<Record<string, string | object> | undefined> {
): Promise<ComfyMetadata | Record<string, string | object> | undefined> {
if (file.type === 'image/png') {
return await getPngMetadata(file)
}
@@ -43,7 +44,7 @@ export async function getWorkflowDataFromFile(
return { workflow, prompt }
}
if (file.type === 'video/webm') {
return (await getFromWebmFile(file)) as unknown as Record<string, object>
return await getFromWebmFile(file)
}
if (
file.name?.endsWith('.mp4') ||
@@ -53,16 +54,13 @@ export async function getWorkflowDataFromFile(
file.type === 'video/quicktime' ||
file.type === 'video/x-m4v'
) {
return (await getFromIsobmffFile(file)) as unknown as Record<string, object>
return await getFromIsobmffFile(file)
}
if (file.type === 'image/svg+xml' || file.name?.endsWith('.svg')) {
return (await getSvgMetadata(file)) as unknown as Record<string, object>
return await getSvgMetadata(file)
}
if (file.type === 'model/gltf-binary' || file.name?.endsWith('.glb')) {
return (await getGltfBinaryMetadata(file)) as unknown as Record<
string,
object
>
return await getGltfBinaryMetadata(file)
}
if (file.name?.endsWith('.latent') || file.name?.endsWith('.safetensors')) {
return await getLatentMetadata(file)