mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: Add generics to typescript generator (#4664)
* feat: Support generating generics in interfaces * Switch struct to a template * Support generics in apitypings
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
# How to add a unit test
|
||||
|
||||
1. Create a new directory in `testdata`
|
||||
2. Name a go file `<directory_name>.go`. This file will generate the typescript.
|
||||
3. Name the expected typescript file `<directory_name>.ts`. This is the unit test's expected output.
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package enums
|
||||
|
||||
type Enum string
|
||||
|
||||
const (
|
||||
EnumFoo Enum = "foo"
|
||||
EnumBar Enum = "bar"
|
||||
EnumBaz Enum = "baz"
|
||||
EnumQux Enum = "qux"
|
||||
)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.
|
||||
|
||||
// From codersdk/enums.go
|
||||
export type Enum = "bar" | "baz" | "foo" | "qux"
|
||||
@@ -0,0 +1,43 @@
|
||||
package generics
|
||||
|
||||
import "time"
|
||||
|
||||
type Single interface {
|
||||
string
|
||||
}
|
||||
|
||||
type Custom interface {
|
||||
string | bool | int | time.Duration | []string | *int
|
||||
}
|
||||
|
||||
// StaticGeneric has all generic fields defined in the field
|
||||
type StaticGeneric struct {
|
||||
Static GenericFields[string, int, time.Duration, string] `json:"static"`
|
||||
}
|
||||
|
||||
// DynamicGeneric can has some dynamic fields
|
||||
type DynamicGeneric[A any, S Single] struct {
|
||||
Dynamic GenericFields[bool, A, string, S] `json:"dynamic"`
|
||||
Comparable bool `json:"comparable"`
|
||||
}
|
||||
|
||||
type ComplexGeneric[C comparable, S Single, T Custom] struct {
|
||||
Dynamic GenericFields[C, bool, string, S] `json:"dynamic"`
|
||||
Order GenericFieldsDiffOrder[C, string, S, T] `json:"order"`
|
||||
Comparable C `json:"comparable"`
|
||||
Single S `json:"single"`
|
||||
Static StaticGeneric `json:"static"`
|
||||
}
|
||||
|
||||
type GenericFields[C comparable, A any, T Custom, S Single] struct {
|
||||
Comparable C `json:"comparable"`
|
||||
Any A `json:"any"`
|
||||
|
||||
Custom T `json:"custom"`
|
||||
Again T `json:"again"`
|
||||
SingleConstraint S `json:"single_constraint"`
|
||||
}
|
||||
|
||||
type GenericFieldsDiffOrder[A any, C comparable, S Single, T Custom] struct {
|
||||
GenericFields[C, A, T, S]
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.
|
||||
|
||||
// From codersdk/generics.go
|
||||
export interface ComplexGeneric<C extends comparable, S extends Single, T extends Custom> {
|
||||
readonly dynamic: GenericFields<C, boolean, string, S>
|
||||
readonly order: GenericFieldsDiffOrder<C, string, S, T>
|
||||
readonly comparable: C
|
||||
readonly single: S
|
||||
readonly static: StaticGeneric
|
||||
}
|
||||
|
||||
// From codersdk/generics.go
|
||||
export interface DynamicGeneric<A extends any, S extends Single> {
|
||||
readonly dynamic: GenericFields<boolean, A, string, S>
|
||||
readonly comparable: boolean
|
||||
}
|
||||
|
||||
// From codersdk/generics.go
|
||||
export interface GenericFields<C extends comparable, A extends any, T extends Custom, S extends Single> {
|
||||
readonly comparable: C
|
||||
readonly any: A
|
||||
readonly custom: T
|
||||
readonly again: T
|
||||
readonly single_constraint: S
|
||||
}
|
||||
|
||||
// From codersdk/generics.go
|
||||
export interface GenericFieldsDiffOrder<A extends any, C extends comparable, S extends Single, T extends Custom> {
|
||||
readonly GenericFields: GenericFields<C, A, T, S>
|
||||
}
|
||||
|
||||
// From codersdk/generics.go
|
||||
export interface StaticGeneric {
|
||||
readonly static: GenericFields<string, number, number, string>
|
||||
}
|
||||
|
||||
// From codersdk/generics.go
|
||||
export type Custom = string | boolean | number | string[] | null
|
||||
|
||||
// From codersdk/generics.go
|
||||
export type Single = string
|
||||
|
||||
export type comparable = boolean | number | string | any
|
||||
Reference in New Issue
Block a user