Skip to content

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Oct 20, 2022

Rebased this PR to main #4658

Also added ability to add unit tests to the generator 👍

Golang

package generics

import "time"

type Single interface {
	string
}

type Custom interface {
        // *int adds the 'null' type
	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[C comparable, A any, S Single] struct {
	Dynamic    GenericFields[C, A, string, S] `json:"dynamic"`
	Comparable C                              `json:"comparable"`
}

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"`
	SingleContraint S `json:"single_constraint"`
}

Typescript

// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.

// From codersdk/generics.go
export interface DynamicGeneric<C extends comparable, A extends any, S extends Single> {
  readonly dynamic: GenericFields<C, A, string, S>
  readonly comparable: C
}

// 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 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

@Emyrk Emyrk requested a review from a team as a code owner October 20, 2022 04:28
@Emyrk Emyrk requested review from jsjoeio and removed request for a team October 20, 2022 04:28
@Emyrk Emyrk requested review from deansheather and f0ssel October 20, 2022 04:32
@Emyrk
Copy link
Member Author

Emyrk commented Oct 20, 2022

@BrunoQuaresma Can you verify that typescript output is reasonable?

@Emyrk Emyrk merged commit 369b5d1 into main Oct 20, 2022
@Emyrk Emyrk deleted the stevenmasley/typescript_gen_generics branch October 20, 2022 13:15
@github-actions github-actions bot locked and limited conversation to collaborators Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants