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
Based on the func_literal integration test, document that Go function
literals are translated into TypeScript arrow functions.
Signed-off-by: Christian Stewart <christian@aperture.us>
Copy file name to clipboardExpand all lines: design/DESIGN.md
+16-2Lines changed: 16 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,7 +90,7 @@ This is the typical package structure of the output TypeScript import path:
90
90
91
91
let i: MyInterface = new MyStruct({ Value: 10 }).clone()
92
92
// goscript.typeAssert returns [value, ok] tuple
93
-
let [s, ok] = goscript.typeAssert<MyStruct>(i, MyStruct) // Runtime helper needed
93
+
let [s, ok] = goscript.typeAssert<MyStruct>(i, MyStruct) // Runtime helper needed
94
94
if (ok) {
95
95
// use s (type MyStruct | null, non-null if ok is true)
96
96
}
@@ -218,6 +218,20 @@ This is the typical package structure of the output TypeScript import path:
218
218
```
219
219
*Note: The reliance on runtime helpers (`@go/builtin`) is crucial for correctly emulating Go's map semantics, especially regarding zero values and potentially type information for `makeMap`.*
220
220
- **Functions:** Converted to TypeScript `function`s. Exported functions are prefixed with `export`.
221
+
- **Function Literals:** Go function literals (anonymous functions) are translated into TypeScript arrow functions (`=>`).
222
+
```go
223
+
greet := func(name string) string {
224
+
return "Hello, " + name
225
+
}
226
+
message := greet("world")
227
+
```
228
+
becomes:
229
+
```typescript
230
+
let greet = (name: string): string => { // Arrow function
231
+
return "Hello, " + name
232
+
}
233
+
let message = greet("world")
234
+
```
221
235
- **Methods:** Go functions with receivers are generated as methods within the corresponding TypeScript `class`. They retain their original Go casing.
222
236
- **Receiver Type (Value vs. Pointer):** Both value receivers (`func (m MyStruct) Method()`) and pointer receivers (`func (m *MyStruct) Method()`) are translated into regular methods on the TypeScript class.
223
237
```go
@@ -473,7 +487,7 @@ To determine which functions need to be marked `async` in TypeScript, the compil
473
487
474
488
### TypeScriptGeneration
475
489
476
-
- **Async Functions:** Go functions colored as **Asynchronous** are generated as TypeScript`async function`s. Theirreturntype`T` is wrapped in a `Promise<T>`. If the function has no return value, the TypeScriptreturntype is `Promise<void>`.
490
+
- **Async Functions:** Go functions colored as **Asynchronous** are generated as TypeScript`async function`s. Theirreturntype`T` is wrapped in a `Promise<T>`. If the function has no return value, the TypeScriptreturntype is `Promise<void>`.
477
491
- **Sync Functions:** Go functions colored as **Synchronous** are generated as regular TypeScript`function`s with their corresponding return types.
478
492
- **Function Calls:** When a Go function call targets an **Asynchronous** function, the generated TypeScript call expression is prefixed with the `await` keyword. Calls to **Synchronous** functions are generated directly without `await`.
0 commit comments