Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Incorrect type conversion for slice of nested struct alias #24

Closed
@elcih17

Description

@elcih17

When converting a slice of a nested struct alias, the type is incorrectly inferred as readonly unknown[] instead of the expected struct type.

Environments
Go version: go version go1.23.8 darwin/arm64
guts version: v1.3.0

Steps to Reproduce:

  1. Add the following Go code to testdata/alias/alias.go:

    package alias
    
    type FooStruct struct {
    	Key string
    }
    
    type AliasStruct = FooStruct
    type AliasStructNested = AliasStruct
    type AliasStructSlice = []FooStruct
    type AliasStructNestedSlice = []AliasStructNested
  2. Run the test for the alias generation:

    go test -run "TestGeneration/alias"

Actual Behavior:

The test fails with the following diff, showing AliasStructNestedSlice is converted to readonly unknown[]:

--- Expected
+++ Actual
@@ -15,2 +15,23 @@
 // From alias/alias.go
+export interface AliasStruct {
+    readonly Key: string;
+}
+
+// From alias/alias.go
+export interface AliasStructNested {
+    readonly Key: string;
+}
+
+// From alias/alias.go
+export type AliasStructNestedSlice = readonly unknown[];
+
+// From alias/alias.go
+export type AliasStructSlice = readonly FooStruct[];
+
+// From alias/alias.go
 export type Foo = string;
+
+// From alias/alias.go
+export interface FooStruct {
+    readonly Key: string;
+}

Specifically, the generated TypeScript for AliasStructNestedSlice is:

// From alias/alias.go
export type AliasStructNestedSlice = readonly unknown[];

Expected Behavior:

The AliasStructNestedSlice should be converted to a slice of the aliased struct type.

Potential Cause and Workaround:

It seems the issue might be related to how types.Alias is handled. In the following code snippet

guts/convert.go

Lines 1006 to 1008 in ad36901

case *types.Alias:
// TODO: Verify this is correct.
return ts.typescriptType(ty.Underlying())

If ty.Underlying() is changed to ty.Rhs(), AliasStructNestedSlice is converted to readonly FooStruct[]. The diff for the test results with this change is:

--- Expected
+++ Actual
@@ -15,2 +15,23 @@
 // From alias/alias.go
+export interface AliasStruct {
+    readonly Key: string;
+}
+
+// From alias/alias.go
+export interface AliasStructNested {
+    readonly Key: string;
+}
+
+// From alias/alias.go
+export type AliasStructNestedSlice = readonly FooStruct[];
+
+// From alias/alias.go
+export type AliasStructSlice = readonly FooStruct[];
+
+// From alias/alias.go
 export type Foo = string;
+
+// From alias/alias.go
+export interface FooStruct {
+    readonly Key: string;
+}

While changing ty.Underlying() to ty.Rhs() resolves the immediate issue of unknown[], I'm unsure whether AliasStructNestedSlice should be converted to readonly AliasStructNested[] or readonly FooStruct[]. Further discussion on the desired behavior for nested aliases would be beneficial.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions