You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5493cb5
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift/Exporting-Swift-Class.md
+48Lines changed: 48 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,53 @@ export type Exports = {
77
77
}
78
78
```
79
79
80
+
## Adding Members via Extensions
81
+
82
+
You can add exported methods, computed properties, and static members to a `@JS` class using extensions. The extension block itself does not need `@JS` - only the individual members do:
83
+
84
+
```swift
85
+
@JS class Greeter {
86
+
@JS var name: String
87
+
88
+
@JS init(name: String) {
89
+
self.name = name
90
+
}
91
+
92
+
@JS func greet() -> String {
93
+
return "Hello, " + self.name + "!"
94
+
}
95
+
}
96
+
97
+
extension Greeter {
98
+
@JS func greetEnthusiastically() -> String {
99
+
return "Hey, " + self.name + "!!!"
100
+
}
101
+
102
+
@JS var nameCount: Int { name.count }
103
+
104
+
@JS static func greetAnonymously() -> String {
105
+
return "Hello."
106
+
}
107
+
108
+
@JS static var defaultGreeting: String { "Hello, world!" }
109
+
}
110
+
```
111
+
112
+
This also works across files within the same module:
113
+
114
+
```swift
115
+
// GreeterExtension.swift
116
+
extension Greeter {
117
+
@JS func greetFormally() -> String {
118
+
return "Good day, " + self.name + "."
119
+
}
120
+
}
121
+
```
122
+
123
+
All `@JS`-annotated members in extensions are merged into the same generated TypeScript interface as the original class declaration.
124
+
125
+
> Note: Extensions must target `@JS`-annotated types from the same module.
126
+
80
127
## How It Works
81
128
82
129
Classes use **reference semantics** when crossing the Swift/JavaScript boundary:
@@ -103,5 +150,6 @@ This differs from structs, which use copy semantics and transfer data by value.
103
150
| Static / class properties: `static var`, `class let` | ✅ (See <doc:Exporting-Swift-Static-Properties> )|
104
151
| Methods: `func` | ✅ (See <doc:Exporting-Swift-Function> ) |
0 commit comments