wails icon indicating copy to clipboard operation
wails copied to clipboard

Typescript interface generation doesn't include null for slice or map

Open alexjacobson95 opened this issue 2 years ago • 2 comments

Description

When wails' ts generation output type is set to interfaces the resulting interfaces do not mark Golang slices as potentially null-able. This can easily lead to front-end code crashing when using the interfaces because there is no reason to do a null check if the interfaces says the field can't be null. Since the default value for a slice in Go is nil, its quite easy to accidentally pass a nil value to Javascript and crash it.

To Reproduce

  1. wails init -n slicetest
  2. Edit wails.json to use "interfaces" type for ts_generation:
{
  ...
  "bindings": {
    "ts_generation": {
      "outputType": "interfaces"
    }
  }
}
  1. Edit main.go to include a slice as one of the types:
package main

import (
	"embed"

	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
)

//go:embed all:frontend/dist
var assets embed.FS

type TestStruct struct {
	TestSlice []int `json:"test_slice"`
}

// App struct
type App struct {
}

func (a *App) NilSlice() TestStruct {
	var t TestStruct
	return t
}

func main() {
	// Create an instance of the app structure
	app := &App{}

	// Create application with options
	err := wails.Run(&options.App{
		Bind: []interface{}{
			app,
		},
	})

	if err != nil {
		println("Error:", err.Error())
	}
}
  1. Run wails generate module
  2. Inspect the resulting models.ts file which uses number[] instead of number[] | null
export namespace main {
	
	export interface TestStruct {
	    test_slice: number[];
	}

}

Expected behaviour

I would expect either the type interface to indicate that a Golang slice could be null when passed to Javascript, or for wails to prevent the Golang Json serializer from converting []int(nil) to null in Json before its passed to JS.

The second option is likely preferred as most of the time it would be annoying to always null check a slice before using it in JS, however that would prevent some use cases. Maybe a setting in the wails.json could be added if we want to go that route.

Screenshots

No response

Attempted Fixes

After looking at this some more, I realized that its because in my project I have the typescript strict setting set true: https://www.typescriptlang.org/tsconfig/#strict

That causes the strictNullChecks setting to be enabled as well, which is why I am expecting the | null in the interface. https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#strictnullchecks-on

I think the best solution would be to provide a flag to the typescript generation section in wails.json (something struct: true) to output the correct interfaces for typescript's strict mode.

System Details

# Wails
Version | v2.9.1

# System
┌────────────────────────────────────────────────────────────────────────┐
| OS           | Windows 10 Pro                                          |
| Version      | 2009 (Build: 19045)                                     |
| ID           | 22H2                                                    |
| Go Version   | go1.21.11                                               |
| Platform     | windows                                                 |
| Architecture | amd64                                                   |
| CPU          | Intel(R) Core(TM) i7-6950X CPU @ 3.00GHz                |
| GPU 1        | NVIDIA TITAN X (Pascal) (NVIDIA) - Driver: 31.0.15.4601 |
| GPU 2        | NVIDIA TITAN X (Pascal) (NVIDIA) - Driver: 31.0.15.4601 |
| Memory       | 32GB                                                    |
└────────────────────────────────────────────────────────────────────────┘

# Dependencies
┌───────────────────────────────────────────────────────┐
| Dependency | Package Name | Status    | Version       |
| WebView2   | N/A          | Installed | 126.0.2592.87 |
| Nodejs     | N/A          | Installed | 20.15.0       |
| npm        | N/A          | Installed | 10.7.0        |
| *upx       | N/A          | Available |               |
| *nsis      | N/A          | Available |               |
└─────────────── * - Optional Dependency ───────────────┘

Additional context

No response

alexjacobson95 avatar Jul 03 '24 11:07 alexjacobson95

Triage: relabelled to v2 — the repro uses the v2 CLI and wails.json ts_generation settings. Verified that the v3 generator already handles this correctly: in interfaces mode it emits \"Names\": string[] | null etc. for slice/map fields (see v3/internal/generator/testdata/output/lang=TS/UseInterfaces=true/.../complex_json/models.ts).

For v2, the fix belongs in v2/internal/typescriptify — slice/map fields need | null in interface output mode since Go zero values serialise as JSON null. Marking as a good first issue; happy to review a PR.

leaanthony avatar Jun 11 '26 03:06 leaanthony

Hi there! Thanks for the report. I've queued this for a reproduction check on Wails v2. We'll keep this open while we investigate.

taliesin-ai avatar Oct 15 '26 19:10 taliesin-ai