The BridgeJS plugin maps TypeScript declarations to Swift using specific macros.
Functions
Exported functions become top-level Swift functions annotated with @JSFunction and throws(JSException).
Global Getters
Module-level declare const or top-level readonly bindings become Swift global properties annotated with @JSGetter.
Classes
Classes become Swift structs annotated with @JSClass:
- Constructor: Becomes
init(...) with @JSFunction. - Properties: Readonly properties use
@JSGetter. Read-writable properties use @JSGetter and @JSSetter (as set<Name>(_:)). - Methods: Become
@JSFunction. - Static Methods: Become
static func on the struct.
Interfaces
Interfaces become Swift structs annotated with @JSClass. They do not have constructors; instances are typically obtained via other function calls returning that interface type. Properties and methods are bridged using @JSGetter, @JSSetter, and @JSFunction.
Type Aliases
- Primitive aliases (e.g.,
type UserId = string): Inlined as the underlying Swift type (e.g., String). - Object-shaped aliases (e.g.,
type User = { id: string }): Emitted as a named Swift struct with @JSClass.
String Enums
TypeScript string enums become Swift enums with String raw values, conforming to necessary BridgeJS protocols.
// TypeScript (bridge-js.d.ts)
export class Greeter {
readonly id: number;
message: string;
constructor(id: string, name: string);
greet(): string;
static createDefault(greetingId: number, locale: string): string;
}
// Generated Swift
@JSClass struct Greeter {
@JSGetter var id: Double
@JSGetter var message: String
@JSSetter func setMessage(_ newValue: String) throws(JSException)
@JSFunction init(id: String, name: String) throws(JSException)
@JSFunction func greet() throws(JSException) -> String
@JSFunction static func createDefault(_ greetingId: Double, _ locale: String) throws(JSException) -> String
}