BridgeJS: Add missing `function` keyword for namespace function declarations in TypeScript by krodak · Pull Request #592 · swiftwasm/JavaScriptKit
Overview
TIL 😅
BridgeJS was generating invalid TypeScript declarations for functions inside namespace blocks. The generated code looked like:
declare global { namespace Utils { namespace String { uppercase(text: string): string; // ❌ Invalid TypeScript } } }
This is a syntax error in TypeScript. Unlike class methods, functions inside TypeScript namespace declarations require the explicit function keyword.
Why this wasn't caught:
JavaScriptKit's tsconfig.json uses "skipLibCheck": true, which skips type-checking of declaration files (.d.ts). The JavaScript E2E tests also passed because JavaScript doesn't have namespace - it only sees the compiled runtime objects.
Project which runs strict TypeScript type-checking (tsc --noEmit) without skipLibCheck fails due to that
Fix
Added the function keyword prefix when generating function declarations inside namespaces:
declare global { namespace Utils { namespace String { function uppercase(text: string): string; // ✅ Valid TypeScript } } }