Skip to content

Commit 8b60eee

Browse files
committed
not yet I guess
1 parent 52ada5b commit 8b60eee

File tree

2 files changed

+1187
-1163
lines changed

2 files changed

+1187
-1163
lines changed

scripts/apitypings/main.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var (
3939
// CLI option types:
4040
"github.com/coder/serpent",
4141
}
42-
indent = "\t"
42+
indent = " "
4343
)
4444

4545
func main() {
@@ -646,7 +646,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
646646
// Just append these as fields. We should fix this later.
647647
state.Fields = append(state.Fields, tsType.AboveTypeLine)
648648
}
649-
state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s;", indent, jsonName, optional, valueType))
649+
state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s", indent, jsonName, optional, valueType))
650650
}
651651

652652
// This is implemented to ensure the correct order of generics on the
@@ -759,8 +759,12 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
759759
// }
760760
// }
761761
return TypescriptType{
762-
ValueType: "unknown",
763-
AboveTypeLine: indentedComment("Embedded anonymous struct, please fix by naming it"),
762+
ValueType: "any",
763+
AboveTypeLine: fmt.Sprintf("%s\n%s",
764+
indentedComment("Embedded anonymous struct, please fix by naming it"),
765+
// Linter needs to be disabled here, or else it will complain about the "any" type.
766+
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- Anonymously embedded struct"),
767+
),
764768
}, nil
765769
case *types.Map:
766770
// map[string][string] -> Record<string, string>
@@ -811,11 +815,16 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
811815
}
812816
genValue := ""
813817

818+
// Always wrap in parentheses for proper scoped types.
819+
// Running prettier on this output will remove redundant parenthesis,
820+
// so this makes our decision-making easier.
821+
// The example that breaks without this is:
822+
// readonly readonly string[][]
814823
if underlying.GenericValue != "" {
815-
genValue = "Readonly<Array<" + underlying.GenericValue + ">>"
824+
genValue = "(readonly " + underlying.GenericValue + "[])"
816825
}
817826
return TypescriptType{
818-
ValueType: "Readonly<Array<" + underlying.ValueType + ">>",
827+
ValueType: "(readonly " + underlying.ValueType + "[])",
819828
GenericValue: genValue,
820829
AboveTypeLine: underlying.AboveTypeLine,
821830
GenericTypes: underlying.GenericTypes,
@@ -960,10 +969,11 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
960969
// You can handle your type manually in the switch list above, otherwise "any" will be used.
961970
// An easy way to fix this is to pull your external type into `codersdk` package, then it will
962971
// be known by the generator.
963-
return TypescriptType{
964-
ValueType: "unknown",
965-
AboveTypeLine: indentedComment(fmt.Sprintf("Named type %q unknown, using \"unknown\"", n.String())),
966-
}, nil
972+
return TypescriptType{ValueType: "any", AboveTypeLine: fmt.Sprintf("%s\n%s",
973+
indentedComment(fmt.Sprintf("Named type %q unknown, using \"any\"", n.String())),
974+
// Linter needs to be disabled here, or else it will complain about the "any" type.
975+
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type"),
976+
)}, nil
967977
}
968978

969979
// Defer to the underlying type.
@@ -992,16 +1002,21 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
9921002
// This field is 'interface{}'. We can't infer any type from 'interface{}'
9931003
// so just use "any" as the type.
9941004
return TypescriptType{
995-
ValueType: "unknown",
996-
AboveTypeLine: indentedComment("Empty interface{} type, cannot resolve the type."),
1005+
ValueType: "any",
1006+
AboveTypeLine: fmt.Sprintf("%s\n%s",
1007+
indentedComment("Empty interface{} type, cannot resolve the type."),
1008+
// Linter needs to be disabled here, or else it will complain about the "any" type.
1009+
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- interface{}"),
1010+
),
9971011
}, nil
9981012
}
9991013

10001014
// Interfaces are difficult to determine the JSON type, so just return
1001-
// an 'unknown'.
1015+
// an 'any'.
10021016
return TypescriptType{
1003-
ValueType: "unknown",
1004-
Optional: false,
1017+
ValueType: "any",
1018+
AboveTypeLine: indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- Golang interface, unable to resolve type."),
1019+
Optional: false,
10051020
}, nil
10061021
case *types.TypeParam:
10071022
_, ok := ty.Underlying().(*types.Interface)

0 commit comments

Comments
 (0)