What is convertValues
in generated models.ts
?
#1433
Replies: 2 comments 4 replies
-
You absolutely do not need to use convertValues. It's a method used internally when passing an object to the constructor. I just use |
Beta Was this translation helpful? Give feedback.
-
You can do constructor(source: Args | any = {}) {
// ..
} but this won't solve the issue because typescript sees type Args = {
name: string;
nested: Nested;
convertValues: (a: any, classs: any, asMap = false) => any; //
} While I don't know if this is possible / easy to do from Go side, but to my knowledge, the ideal solution will be:
for example, if the Go's code looks like this: type Nested struct {
Id int `json:"id"`
}
type Args struct {
Name string `json:"name"`
Nested Nested `json:"nested"`
}
func (a *App) Example(args Args) {
// ...
} the generated typescript code will look like this: // App.d.ts
function Example(arg1: main.Args): void;
// models.ts
export namespace main {
export interface NestedProps { // converted type from Nested struct in Go
id: number;
}
export class Nested {
id: number;
static createFrom(source: NestedProps) {
return new Nested(source);
}
constructor(source: NestedProps | string) {
if (typeof source === "string") source = JSON.parse(source) as NestedProps;
this.id = source["id"];
}
}
export interface ArgsProps { // converted type from Args struct in Go
name: string;
nested: NestedProps;
}
export class Args {
name: string;
// Go type: Nested
nested: Nested;
static createFrom(source: ArgsProps) {
return new Args(source);
}
constructor(source: ArgsProps | string) {
if (typeof source === "string") source = JSON.parse(source) as ArgsProps;
this.name = source["name"];
this.nested = this.convertValues(source["nested"], Nested);
}
convertValues(a: any, classs: any, asMap = false): any {
// ...
}
}
}
// usage
Example(main.Args.createFrom({
name: "foo",
nested: {
id: 1,
},
})); |
Beta Was this translation helpful? Give feedback.
-
I have a simple Go method that's being exposed to the frontend, it looks like this:
The generated typescript code on
models.ts
andApp.d.ts
looks like thisMy questions are:
convertValues
onmain.Nested
? When I'm trying to callExample
method from the frontend, it requires me to passconvertValues
as the argsforcing me to create a new
Args
instance instead like this:but then I lost typing feature because
main.Args
's constructor parameter isany
.nested
property inArgs
isany
instead ofmain.Nested
?Beta Was this translation helpful? Give feedback.
All reactions