Skip to content

Conversation

@atterpac
Copy link
Member

@atterpac atterpac commented Jan 28, 2026

Description

Adds an opt-in flag to enable null type generation for slices. On master if you have

func (a *App) Greet() []string {
    var defaultNil string
    return defaultNil
}

Generates

export function Greet(arg1:string):Promise<Array<string>>;

Since slices are default nil in golang makes sense that the binding generation would be able to support nullable vs undefined (which is currently covered with fieldName? on omitempty

This could cause breaking changes if set as default behavior due to typescript error on null checks, so I made it opt in via wails.json

"bindings": {
  "ts_generation": {
    "useNullableSlices": true
  }
}

It will then output

export function Greet(arg1:string):Promise<Array<string> | null>;

Fixes # (issue)
Issues reported in discord community/direct

Type of change

Please select the option that is relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

  • Windows
  • macOS
  • Linux

If you checked Linux, please specify the distro and version.

Test Configuration

# Wails
Version  | v2.11.0
Revision | cd8952781dad1e6f2aeddfca6e8020b9cdda5e7c
Modified | true


# System
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
| OS           | MacOS                                                                                                                       |
| Version      | 15.7.3                                                                                                                      |
| ID           | 24G419                                                                                                                      |
| Branding     |                                                                                                                             |
| Go Version   | go1.25.2                                                                                                                    |
| Platform     | darwin                                                                                                                      |
| Architecture | arm64                                                                                                                       |
| CPU 1        | Apple M4 Max                                                                                                                |
| CPU 2        | Apple M4 Max                                                                                                                |
| GPU          | Chipset Model: Apple M4 Max Type: GPU Bus: Built-In Total Number of Cores: 32 Vendor: Apple (0x106b) Metal Support: Metal 3 |
| Memory       | 36GB                                                                                                                        |
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

# Dependencies
┌────────────────────────────────────────────────────────────────┐
| Dependency                | Package Name | Status    | Version |
| Xcode command line tools  | N/A          | Installed | 2410    |
| Nodejs                    | N/A          | Installed | 22.20.0 |
| npm                       | N/A          | Installed | 10.9.3  |
| *Xcode                    | N/A          | Available |         |
| *upx                      | N/A          | Available |         |
| *nsis                     | N/A          | Available |         |
|                                                                |
└─────────────────── * - Optional Dependency ────────────────────┘

# Diagnosis
Optional package(s) installation details:
  - Xcode: Available at https://apps.apple.com/us/app/xcode/id497799835
  - upx : Available at https://upx.github.io/
  - nsis : More info at https://wails.io/docs/guides/windows-installer/

Summary by CodeRabbit

  • New Features

    • Introduced opt-in nullable slices feature for TypeScript generation. When enabled via CLI flag or environment variable, array types are rendered as nullable in generated code.
  • Documentation

    • Added changelog entry documenting the new nullable slices capability.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 28, 2026

📝 Walkthrough

Walkthrough

This PR introduces a new UseNullableSlices feature flag to allow TypeScript array/slice types to be generated as nullable. The flag is added to project configuration, propagated through CLI flags and environment variables, wired through the bindings generation pipeline, and implemented in type conversion functions via receiver method refactoring.

Changes

Cohort / File(s) Change Summary
Project Configuration
v2/internal/project/project.go
Added UseNullableSlices boolean field to TsGeneration struct with JSON serialization as useNullableSlices.
CLI & Environment Integration
v2/internal/app/app_bindings.go
Introduced new CLI flag useNullableSlicesFlag and environment variable parsing; added SetUseNullableSlices() method to propagate flag into bindings generator.
Bindings Configuration
v2/internal/binding/binding.go
Added useNullableSlices field and public SetUseNullableSlices() setter; propagated flag through model generation via WithUseNullableSlices().
Type Conversion & Nullable Logic
v2/internal/binding/generate.go
Converted goTypeToTypescriptType and goTypeToJSDocType to receiver methods; updated arrayifyValue signature to accept useNullableSlices parameter; appends `"
Test Coverage
v2/internal/binding/generate_test.go
Refactored existing test to use method-based calls on Bindings instance; added new Test_goTypeToJSDocType_nullable function to validate nullable slice handling.
TypeScript Generation
v2/internal/typescriptify/typescriptify.go
Added UseNullableSlices field and WithUseNullableSlices() setter; integrated flag into typeScriptClassBuilder to render array fields with nullable suffix.
Bindings API & Commands
v2/pkg/commands/bindings/bindings.go, v2/pkg/commands/build/build.go
Added UseNullableSlices option to Options struct; wired propagation through environment variables and build command to bindings generation.
Build Command Integration
v2/cmd/wails/generate.go
Passed UseNullableSlices option from project config to bindings.GenerateBindings call.
Documentation
website/src/pages/changelog.mdx
Added changelog entries documenting new nullableSlices opt-in feature.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

Enhancement, go, size:L, lgtm

Suggested reviewers

  • leaanthony

Poem

🐰 A flag hops through configs with glee,
Making slices nullable, wild and free!
From CLI to types, the feature takes flight,
Arrays wrapped in null—TypeScript delight! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Opt-in nullable slices' clearly and concisely describes the primary change: adding an opt-in feature for nullable slice type generation in TypeScript bindings.
Description check ✅ Passed The pull request description provides a clear summary of changes, includes relevant motivation (Go slices default to nil), shows configuration examples, and provides test configuration details. All major template sections are addressed.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
v2/internal/typescriptify/typescriptify.go (1)

281-344: AddMapField missing nullable suffix for slice value types.

When useNullableSlices is enabled, AddMapField doesn't apply the nullable suffix to slice value types. Compare lines 291-301 (where slice values are handled) with AddArrayOfStructsField (lines 949-956) and AddSliceField (lines 873-878)—both correctly apply nullableSuffix = " | null" when t.useNullableSlices is true. Map fields with slice values (e.g., map[string][]int, map[string][][]int) generate Record<string, Array<...>> instead of Record<string, Array<...> | null>, inconsistent with direct slice field handling. This affects test cases like those in binding_deepelements_test.go (lines 17-30).

🤖 Fix all issues with AI agents
In `@website/src/pages/changelog.mdx`:
- Line 36: The changelog line mentioning the `nullableSlices` opt-in currently
placed under v2.11.0 should be moved to the "Unreleased → Added" section unless
this was intentionally backported; verify whether this PR (the `nullableSlices`
entry referencing `#4920`) is a backport, and if not, cut the line "Added
`nullableSlices` opt-in to allow nullable array type generation in
[`#4920`](https://github.com/wailsapp/wails/pull/4920)" from its current v2.11.0
location in website/src/pages/changelog.mdx and paste it under the Unreleased →
Added subsection so the entry appears in the correct release section.
🧹 Nitpick comments (2)
v2/internal/typescriptify/typescriptify.go (1)

874-878: Consider extracting duplicated nullable suffix logic.

The nullable suffix construction (" | null") is duplicated in both AddSimpleArrayField and AddArrayOfStructsField. Consider extracting this into a helper method for consistency and maintainability.

♻️ Suggested refactor
// Add helper method to typeScriptClassBuilder
func (t *typeScriptClassBuilder) nullableSuffix() string {
    if t.useNullableSlices {
        return " | null"
    }
    return ""
}

Then use t.nullableSuffix() in both methods.

Also applies to: 951-955

v2/internal/binding/generate_test.go (1)

153-184: Good test coverage for nullable slices.

The test correctly validates:

  • []intArray<number> | null
  • []boolArray<boolean> | null
  • []bytestring (special case preserved)

Consider adding test cases for nested arrays (e.g., [][]int) and maps with slice values to ensure complete coverage.

💡 Additional test cases to consider
{
    name:  "nested slice nullable",
    input: "[][]int",
    want:  "Array<Array<number> | null> | null", // verify expected behavior
},
{
    name:  "map with slice value nullable",
    input: "map[string][]int",
    want:  "Record<string, Array<number> | null>", // verify expected behavior
},


### Added

- Added `nullableSlices` opt-in to allow nullable array type generation in [#4920](https://github.com/wailsapp/wails/pull/4920)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Move this entry to “Unreleased → Added” unless it’s a backport.

This PR was opened on January 28, 2026, while v2.11.0 is dated November 8, 2025, so the changelog entry likely belongs under the Unreleased “Added” section. Please confirm if this is intentionally backported to v2.11.0; otherwise, relocate it.

📌 Suggested edit (if not a backport)
 ## [Unreleased]
 
+### Added
+
+- Added `nullableSlices` opt-in to allow nullable array type generation in [`#4920`](https://github.com/wailsapp/wails/pull/4920)
+
 ### Fixed
@@
-### Added
-
-- Added `nullableSlices` opt-in to allow nullable array type generation in [`#4920`](https://github.com/wailsapp/wails/pull/4920)
🤖 Prompt for AI Agents
In `@website/src/pages/changelog.mdx` at line 36, The changelog line mentioning
the `nullableSlices` opt-in currently placed under v2.11.0 should be moved to
the "Unreleased → Added" section unless this was intentionally backported;
verify whether this PR (the `nullableSlices` entry referencing `#4920`) is a
backport, and if not, cut the line "Added `nullableSlices` opt-in to allow
nullable array type generation in
[`#4920`](https://github.com/wailsapp/wails/pull/4920)" from its current v2.11.0
location in website/src/pages/changelog.mdx and paste it under the Unreleased →
Added subsection so the entry appears in the correct release section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant