-
-
Notifications
You must be signed in to change notification settings - Fork 57
Description
Motivation
Currently, importing JavaScript APIs from the global scope requires users to write TypeScript declaration files and import APIs through the standard BridgeJS workflow. For simple use cases where users only need to access a few well-known global APIs like console.log() or document.getElementById(), this approach can feel heavyweight and requires additional setup steps.
Proposed Solution
Allow users to declare global scope imports directly in Swift using @JS var declarations with protocol interfaces, eliminating the need for separate .d.ts files for simple global API access.
High-level API
@JS protocol JSConsole {
func log(_ message: String)
func error(_ message: String)
}
@JS var console: JSConsole
@JS protocol JSElement {
var innerHTML: String { get set }
func addEventListener(_ event: String, _ handler: @escaping () -> Void)
}
@JS protocol JSDocument {
func getElementById(_ id: String) -> JSElement?
}
@JS(from: .global) var document: JSDocument
@JS(from: .module("uuid"), jsName: "v4")
func uuidv4() -> StringImplementation
This feature would require adding a macro plugin to support @JS var [name]: [Type] declarations that generates getter/setter accessors interacting with the code generated by @JS protocol
If it works well, we might be able to make ImportTS just to generate @JS annotated Swift code and centralize ABI-facing code in ExportSwift