Skip to content

Commit 8e1e66e

Browse files
committed
docs: document func literal translation
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>
1 parent c301174 commit 8e1e66e

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

design/DESIGN.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ This is the typical package structure of the output TypeScript import path:
9090
9191
let i: MyInterface = new MyStruct({ Value: 10 }).clone()
9292
// goscript.typeAssert returns [value, ok] tuple
93-
let [s, ok] = goscript.typeAssert&lt;MyStruct&gt;(i, MyStruct) // Runtime helper needed
93+
let [s, ok] = goscript.typeAssert<MyStruct>(i, MyStruct) // Runtime helper needed
9494
if (ok) {
9595
// use s (type MyStruct | null, non-null if ok is true)
9696
}
@@ -218,6 +218,20 @@ This is the typical package structure of the output TypeScript import path:
218218
```
219219
*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`.*
220220
- **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+
```
221235
- **Methods:** Go functions with receivers are generated as methods within the corresponding TypeScript `class`. They retain their original Go casing.
222236
- **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.
223237
```go
@@ -473,7 +487,7 @@ To determine which functions need to be marked `async` in TypeScript, the compil
473487

474488
### TypeScript Generation
475489

476-
- **Async Functions:** Go functions colored as **Asynchronous** are generated as TypeScript `async function`s. Their return type `T` is wrapped in a `Promise&lt;T&gt;`. If the function has no return value, the TypeScript return type is `Promise&lt;void&gt;`.
490+
- **Async Functions:** Go functions colored as **Asynchronous** are generated as TypeScript `async function`s. Their return type `T` is wrapped in a `Promise<T>`. If the function has no return value, the TypeScript return type is `Promise<void>`.
477491
- **Sync Functions:** Go functions colored as **Synchronous** are generated as regular TypeScript `function`s with their corresponding return types.
478492
- **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`.
479493

0 commit comments

Comments
 (0)