gzuidhof/tygo

Generate fields for types that are composed of subtypes

petterhs opened this issue · 2 comments

Hi! Thank you for your generator!

I'm trying to generate types where I have a type that is composed of subtypes. Is there something I can do in order for this to work.

Example:

type Base struct {
    Name   string  `json:"name"`
}

type Other struct {
    Base
    OtherField  string `json:"otherField"`
}

Expected output:

export interface Other {
  name: string;
  otherField: string;
}

Hey Petter,

I think in the current setup it's tricky, as it's more or less a "one-pass" compiler, whereas this requires backtracking or somehow stepping back. Which is possible (tygo should probably have some intermediate state then so it can reason about these things), but requires significant effort.

As for expected output, maybe extends X would be better semantically?

export interface Base {
  name: string
}

export interface Other extends Base {
  otherField: string;
}

I fear Go's rules when it comes to having fields with the same name, I'm not sure how equivalent those are in Typescript.


One thing that may just work, and doesn't require as much engineering is for the AST-walker to remember which interfaces it extends, and add & Base at the end. But then the output in TS has to be a type instead of interface... Just thinking out loud!

Long story short:
Right now it's not possible with Tygo, and supporting it requires effort. If feasible at all I would propose expanding the types manually in Go.

Will close this now that #30 landed in release 0.2.7