Improve type checking utility to handle opaque external types (e.g., DOM elements)

This commit is contained in:
2026-04-27 16:05:43 +02:00
parent 54e16d44b4
commit d62bc911a3

View File

@@ -155,6 +155,10 @@ const TYPED_ARRAY_TYPE_NAMES = new Set([
"Uint16Array", "Uint16Array",
"Uint32Array" "Uint32Array"
]); ]);
const OPAQUE_EXTERNAL_TYPE_NAMES = new Set([
"HTMLCanvasElement",
"PerspectiveCamera"
]);
const repoRoot = process.cwd(); const repoRoot = process.cwd();
const configPath = ts.findConfigFile(repoRoot, ts.sys.fileExists, "tsconfig.json"); const configPath = ts.findConfigFile(repoRoot, ts.sys.fileExists, "tsconfig.json");
@@ -338,17 +342,25 @@ function isRepoSourceFile(fileName: string): boolean {
); );
} }
function isExternalObjectType(type: ts.Type): boolean { function isOpaqueExternalObjectType(type: ts.Type): boolean {
const normalizedType = withoutNullish(type); const normalizedType = withoutNullish(type);
const symbol = normalizedType.aliasSymbol ?? normalizedType.symbol; const symbol = normalizedType.aliasSymbol ?? normalizedType.symbol;
const symbolName = String(symbol?.escapedName ?? "");
const declarations = symbol?.declarations ?? []; const declarations = symbol?.declarations ?? [];
if (declarations.length === 0) { if (declarations.length === 0) {
return false; return false;
} }
return declarations.every( return (
(declaration) => !isRepoSourceFile(declaration.getSourceFile().fileName) OPAQUE_EXTERNAL_TYPE_NAMES.has(symbolName) ||
declarations.some((declaration) => {
const fileName = declaration.getSourceFile().fileName;
return (
fileName.includes(`${path.sep}node_modules${path.sep}`) ||
fileName.endsWith(`${path.sep}lib.dom.d.ts`)
);
})
); );
} }
@@ -536,7 +548,7 @@ function collectFields(
return; return;
} }
if (isExternalObjectType(normalizedType)) { if (isOpaqueExternalObjectType(normalizedType)) {
entries.push({ entries.push({
path: currentPath, path: currentPath,
condition: options.condition condition: options.condition