Skip to content

Map-read uses unimported symbol resulting in ReferenceError #134

@attila-kun

Description

@attila-kun

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:

bar

Actual 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 defined

I've prepared the same example here: https://github.com/attila-kun/goscriptbug

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions