Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript generation of Generic Struct #2323

Closed
aaronflorey opened this issue Jan 21, 2023 · 5 comments · Fixed by #3978
Closed

Typescript generation of Generic Struct #2323

aaronflorey opened this issue Jan 21, 2023 · 5 comments · Fixed by #3978
Labels
TODO The issue is ready to be developed v3

Comments

@aaronflorey
Copy link

aaronflorey commented Jan 21, 2023

Description

When using a generic struct as a return type of a public function, it generates invalid typescript

To Reproduce

Go

type Collection[T interface{}] struct {
	Data []T `json:"data"`
}

type Item struct {
	Id int `json:"id"`
}

func (a *App) GetTestData() Collection[Item] {

	var result Collection[Item]

	return result
}

# OR

type ItemCollection = Collection[Item]

func (a *App) GetTestData() ItemCollection {

	var result ItemCollection

	return result
}

model.ts

	export class Collection[changeme/backend.Item] {
	    data: Item[];
	
	    static createFrom(source: any = {}) {
	        return new Collection[changeme/backend.Item](source);
	    }
	
	    constructor(source: any = {}) {
	        if ('string' === typeof source) source = JSON.parse(source);
	        this.data = this.convertValues(source["data"], Item);
	    }
	
		convertValues(a: any, classs: any, asMap: boolean = false): any {
		    if (!a) {
		        return a;
		    }
		    if (a.slice) {
		        return (a as any[]).map(elem => this.convertValues(elem, classs));
		    } else if ("object" === typeof a) {
		        if (asMap) {
		            for (const key of Object.keys(a)) {
		                a[key] = new classs(a[key]);
		            }
		            return a;
		        }
		        return new classs(a);
		    }
		    return a;
		}
	}

Expected behaviour

It should generate a new generic type and use that instead

Screenshots

No response

Attempted Fixes

The only way to get around it is to return interface{} or something else that doesn't require generics.

I tried this package and it was able to successfully generate the generics. Could possibly utilise that. github.com/gzuidhof/tygo

System Details

Scanning system - Please wait (this may take a long time)...Done.

# System

OS           | MacOS   
Version      | 12.3    
ID           | 21E230  
Go Version   | go1.19.5
Platform     | darwin  
Architecture | arm64   

# Wails

Version | v2.3.1

# Dependencies

Dependency                | Package Name | Status    | Version
Xcode command line tools  | N/A          | Installed | 2395   
npm                       | N/A          | Installed | 8.12.1 
*Xcode                    | N/A          | Available |        
*upx                      | N/A          | Available |        
*nsis                     | N/A          | Available |        
* - Optional Dependency

# Diagnosis

Your system is ready for Wails development!

Additional context

No response

@aaronflorey aaronflorey added the Bug Something isn't working label Jan 21, 2023
@leaanthony
Copy link
Member

Tygo looks great. I'll look to see if we could just use that instead.

@KiddoV
Copy link
Contributor

KiddoV commented Aug 20, 2024

Any update on this?

@leaanthony
Copy link
Member

Did you try using tygo to generate?

@KiddoV
Copy link
Contributor

KiddoV commented Aug 21, 2024

I haven't try that. We can wails generate module with tygo?

It looks promising. With this, I can match all of my Golang types with TS.

I think we should add a document for using Wails with Tygo.

@leaanthony
Copy link
Member

This won't be supported in v2 but will keep this open as a TODO for v3.

@leaanthony leaanthony added TODO The issue is ready to be developed v3 and removed Bug Something isn't working labels Nov 18, 2024
@coderabbitai coderabbitai bot mentioned this issue Feb 10, 2025
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TODO The issue is ready to be developed v3
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants