-
Notifications
You must be signed in to change notification settings - Fork 10
Map-read uses unimported symbol resulting in ReferenceError #134
Copy link
Copy link
Open
Labels
Description
Thank you for this amazing project!
Ran into a bug today:
Example
This bug needs at least 2 files to exist:
src/types.go
type Foo struct {
Value string
}
var Storage = map[string]Foo{
"foo": {Value: "bar"},
}src/read.go
func ReadValue(key string) string {
return Storage[key].Value
}Expected behavior
The generated code should be runnable, and this call:
console.log(ReadValue("foo"));should print:
barActual behaviour
A plain Go map read:
func ReadValue(key string) string {
return Storage[key].Value
}is compiled into TypeScript that calls mapGet with new Foo() as the zero-value fallback:
export function ReadValue(key: string): string {
return $.mapGet(Storage, key, new Foo())[0].Value
}However, the generated file does not import Foo, so the code crashes at runtime with:
ReferenceError: Foo is not definedI've prepared the same example here: https://github.com/attila-kun/goscriptbug
Reactions are currently unavailable