diff --git a/convert.go b/convert.go index 64151f3..8bfaa3d 100644 --- a/convert.go +++ b/convert.go @@ -494,26 +494,41 @@ func (ts *Typescript) parse(obj types.Object) error { // TODO: Are any enums var declarations? This is also codersdk.Me. return nil // Maybe we should treat these like consts? case *types.Const: - // Names are very likely enums - named, ok := obj.Type().(*types.Named) - if !ok { - // It could be a raw const value to generate. - if _, ok := obj.Type().(*types.Basic); ok { - cnst, err := ts.constantDeclaration(obj) - if err != nil { - return xerrors.Errorf("basic const %q: %w", objectIdentifier.Ref(), err) + type constMethods interface { + Obj() *types.TypeName + Underlying() types.Type + } + + var use constMethods + { // TODO: This block could be cleaned up + // Names & aliases are very likely enums + named, namedOk := obj.Type().(*types.Named) + aliased, aliasOk := obj.Type().(*types.Alias) + + if !namedOk && !aliasOk { + // It could be a raw const value to generate. + if _, ok := obj.Type().(*types.Basic); ok { + cnst, err := ts.constantDeclaration(obj) + if err != nil { + return xerrors.Errorf("basic const %q: %w", objectIdentifier.Ref(), err) + } + return ts.setNode(objectIdentifier.Ref(), typescriptNode{ + Node: cnst, + }) } - return ts.setNode(objectIdentifier.Ref(), typescriptNode{ - Node: cnst, - }) + return xerrors.Errorf("const %q is not a named type", objectIdentifier.Ref()) + } + if namedOk { + use = named + } else { + use = aliased } - return xerrors.Errorf("const %q is not a named type", objectIdentifier.Ref()) } // Treat it as an enum. - enumObjName := ts.parsed.Identifier(named.Obj()) + enumObjName := ts.parsed.Identifier(use.Obj()) - switch named.Underlying().(type) { + switch use.Underlying().(type) { case *types.Basic: default: return xerrors.Errorf("const %q is not a basic type, enums only support basic", objectIdentifier.Ref()) diff --git a/testdata/enumtypes/enumtypes.go b/testdata/enumtypes/enumtypes.go index d13ec85..460e13b 100644 --- a/testdata/enumtypes/enumtypes.go +++ b/testdata/enumtypes/enumtypes.go @@ -26,3 +26,12 @@ const ( AudienceTenant Audience = "tenant" AudienceTeam Audience = "team" ) + +type EnumAlias = string + +const ( + EnumAliasString EnumAlias = "string" + EnumAliasNumber EnumAlias = "number" + EnumAliasBoolean EnumAlias = "bool" + EnumAliasListString EnumAlias = "list(string)" +) diff --git a/testdata/enumtypes/enumtypes.ts b/testdata/enumtypes/enumtypes.ts index cfe2902..2771035 100644 --- a/testdata/enumtypes/enumtypes.ts +++ b/testdata/enumtypes/enumtypes.ts @@ -5,6 +5,11 @@ export type Audience = "team" | "tenant" | "world"; export const Audiences: Audience[] = ["team", "tenant", "world"]; +// From enumtypes/enumtypes.go +export type EnumAlias = "bool" | "list(string)" | "number" | "string"; + +export const EnumAliases: EnumAlias[] = ["bool", "list(string)", "number", "string"]; + // From enumtypes/enumtypes.go export type EnumInt = 10 | 5;