Skip to content

Commit e55d921

Browse files
authored
chore: Typescript generator TODOs resolved, adding explainations (#6633)
* chore: Explain usage of eslint comments * Conform comment * Fix wording * Linting
1 parent b1c1e1a commit e55d921

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

scripts/apitypings/main.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
633633
}
634634
case *types.Struct:
635635
// This handles anonymous structs. This should never happen really.
636+
// If you require this, either change your datastructures, or implement
637+
// anonymous structs here.
636638
// Such as:
637639
// type Name struct {
638640
// Embedded struct {
@@ -643,7 +645,8 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
643645
ValueType: "any",
644646
AboveTypeLine: fmt.Sprintf("%s\n%s",
645647
indentedComment("Embedded anonymous struct, please fix by naming it"),
646-
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed"),
648+
// Linter needs to be disabled here, or else it will complain about the "any" type.
649+
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- Anonymously embedded struct"),
647650
),
648651
}, nil
649652
case *types.Map:
@@ -766,9 +769,14 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
766769

767770
// If it's a struct, just use the name of the struct type
768771
if _, ok := n.Underlying().(*types.Struct); ok {
772+
// External structs cannot be introspected, as we only parse the codersdk package.
773+
// You can handle your type manually in the switch list above, otherwise "any" will be used.
774+
// An easy way to fix this is to pull your external type into `codersdk` package, then it will
775+
// be known by the generator.
769776
return TypescriptType{ValueType: "any", AboveTypeLine: fmt.Sprintf("%s\n%s",
770777
indentedComment(fmt.Sprintf("Named type %q unknown, using \"any\"", n.String())),
771-
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed"),
778+
// Linter needs to be disabled here, or else it will complain about the "any" type.
779+
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type"),
772780
)}, nil
773781
}
774782

@@ -789,14 +797,28 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
789797
resp.Optional = true
790798
return resp, nil
791799
case *types.Interface:
792-
// only handle the empty interface for now
800+
// only handle the empty interface (interface{}) for now
793801
intf := ty
794802
if intf.Empty() {
803+
// This field is 'interface{}'. We can't infer any type from 'interface{}'
804+
// so just use "any" as the type.
795805
return TypescriptType{
796-
ValueType: "any",
797-
AboveTypeLine: indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed"),
806+
ValueType: "any",
807+
AboveTypeLine: fmt.Sprintf("%s\n%s",
808+
indentedComment("Empty interface{} type, cannot resolve the type."),
809+
// Linter needs to be disabled here, or else it will complain about the "any" type.
810+
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- interface{}"),
811+
),
798812
}, nil
799813
}
814+
// All complex interfaces should be named. So if we get here, that means
815+
// we are using anonymous interfaces. Which is just weird and not supported.
816+
// Example:
817+
// type Foo struct {
818+
// Bar interface {
819+
// Baz() string
820+
// }
821+
// }
800822
return TypescriptType{}, xerrors.New("only empty interface types are supported")
801823
case *types.TypeParam:
802824
_, ok := ty.Underlying().(*types.Interface)

site/src/api/typesGenerated.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ export type AuditDiff = Record<string, AuditDiffField>
5353

5454
// From codersdk/audit.go
5555
export interface AuditDiffField {
56-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
56+
// Empty interface{} type, cannot resolve the type.
57+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- interface{}
5758
readonly old?: any
58-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
59+
// Empty interface{} type, cannot resolve the type.
60+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- interface{}
5961
readonly new?: any
6062
readonly secret: boolean
6163
}
@@ -67,7 +69,7 @@ export interface AuditLog {
6769
readonly time: string
6870
readonly organization_id: string
6971
// Named type "net/netip.Addr" unknown, using "any"
70-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
72+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type
7173
readonly ip: any
7274
readonly user_agent: string
7375
readonly resource_type: ResourceType
@@ -357,13 +359,13 @@ export interface DeploymentValues {
357359
readonly disable_password_auth?: boolean
358360
readonly support?: SupportConfig
359361
// Named type "github.com/coder/coder/cli/clibase.Struct[[]github.com/coder/coder/codersdk.GitAuthConfig]" unknown, using "any"
360-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
362+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type
361363
readonly git_auth?: any
362364
readonly config_ssh?: SSHConfig
363365
readonly config?: string
364366
readonly write_config?: boolean
365367
// Named type "github.com/coder/coder/cli/clibase.HostPort" unknown, using "any"
366-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
368+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type
367369
readonly address?: any
368370
}
369371

@@ -442,7 +444,8 @@ export interface License {
442444
readonly id: number
443445
readonly uuid: string
444446
readonly uploaded_at: string
445-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
447+
// Empty interface{} type, cannot resolve the type.
448+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- interface{}
446449
readonly claims: Record<string, any>
447450
}
448451

@@ -578,15 +581,15 @@ export interface PatchGroupRequest {
578581
export interface PprofConfig {
579582
readonly enable: boolean
580583
// Named type "github.com/coder/coder/cli/clibase.HostPort" unknown, using "any"
581-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
584+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type
582585
readonly address: any
583586
}
584587

585588
// From codersdk/deployment.go
586589
export interface PrometheusConfig {
587590
readonly enable: boolean
588591
// Named type "github.com/coder/coder/cli/clibase.HostPort" unknown, using "any"
589-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
592+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type
590593
readonly address: any
591594
}
592595

@@ -683,7 +686,8 @@ export interface SSHConfigResponse {
683686
// From codersdk/serversentevents.go
684687
export interface ServerSentEvent {
685688
readonly type: ServerSentEventType
686-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
689+
// Empty interface{} type, cannot resolve the type.
690+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- interface{}
687691
readonly data: any
688692
}
689693

@@ -705,7 +709,7 @@ export interface SessionCountDeploymentStats {
705709
// From codersdk/deployment.go
706710
export interface SupportConfig {
707711
// Named type "github.com/coder/coder/cli/clibase.Struct[[]github.com/coder/coder/codersdk.LinkConfig]" unknown, using "any"
708-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
712+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type
709713
readonly links: any
710714
}
711715

@@ -718,7 +722,7 @@ export interface SwaggerConfig {
718722
export interface TLSConfig {
719723
readonly enable: boolean
720724
// Named type "github.com/coder/coder/cli/clibase.HostPort" unknown, using "any"
721-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
725+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type
722726
readonly address: any
723727
readonly redirect_http: boolean
724728
readonly cert_file: string[]

0 commit comments

Comments
 (0)