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:
Steven Masley
2022-10-20 08:15:24 -05:00
committed by GitHub
parent d0b1c36d51
commit 369b5d1c2d
9 changed files with 598 additions and 91 deletions
+5
View File
@@ -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
View File
@@ -0,0 +1,10 @@
package enums
type Enum string
const (
EnumFoo Enum = "foo"
EnumBar Enum = "bar"
EnumBaz Enum = "baz"
EnumQux Enum = "qux"
)
+4
View File
@@ -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"
+43
View File
@@ -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]
}
+43
View File
@@ -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