Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: update generated array type definitions in TypeScript to be readonly#12947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Parkreiner merged 11 commits intomainfromstevenmasley/readonly_gen
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
11 commits
Select commitHold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletionsscripts/apitypings/main.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -814,11 +814,17 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
return TypescriptType{}, xerrors.Errorf("array: %w", err)
}
genValue := ""

// Always wrap in parentheses for proper scoped types.
// Running prettier on this output will remove redundant parenthesis,
// so this makes our decision-making easier.
// The example that breaks without this is:
//readonly readonly string[][]
if underlying.GenericValue != "" {
genValue = underlying.GenericValue + "[]"
genValue ="(readonly " +underlying.GenericValue + "[])"
}
return TypescriptType{
ValueType: underlying.ValueType + "[]",
ValueType:"(readonly " +underlying.ValueType + "[])",
GenericValue: genValue,
AboveTypeLine: underlying.AboveTypeLine,
GenericTypes: underlying.GenericTypes,
Expand Down
12 changes: 11 additions & 1 deletionscripts/apitypings/main_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
package main

import (
"flag"
"os"
"path/filepath"
"strings"
Expand All@@ -15,6 +16,9 @@ import (
"github.com/stretchr/testify/require"
)

// updateGoldenFiles is a flag that can be set to update golden files.
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")

func TestGeneration(t *testing.T) {
t.Parallel()
files, err := os.ReadDir("testdata")
Expand All@@ -37,7 +41,13 @@ func TestGeneration(t *testing.T) {
require.NoErrorf(t, err, "read file %s", golden)
expectedString := strings.TrimSpace(string(expected))
output = strings.TrimSpace(output)
require.Equal(t, expectedString, output, "matched output")
if *updateGoldenFiles {
// nolint:gosec
err := os.WriteFile(golden, []byte(output), 0o644)
require.NoError(t, err, "write golden file")
} else {
require.Equal(t, expectedString, output, "matched output")
}
})
}
}
4 changes: 2 additions & 2 deletionsscripts/apitypings/testdata/enums/enums.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
package codersdk

type (
Enum string
Enums []Enum
Enumstring
EnumSliceType []Enum
)

const (
Expand Down
2 changes: 1 addition & 1 deletionscripts/apitypings/testdata/enums/enums.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
// From codersdk/enums.go
export typeEnums = Enum[]
export typeEnumSliceType =(readonlyEnum[])

// From codersdk/enums.go
export type Enum = "bar" | "baz" | "foo" | "qux"
Expand Down
12 changes: 6 additions & 6 deletionsscripts/apitypings/testdata/genericmap/genericmap.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
package codersdk

type Foo struct {
Bar string `json:"bar"`
}

type Buzz struct {
Foo `json:"foo"`
Bazz string `json:"bazz"`
}

typeCustom interface {
Foo | Buzz
typeFoo struct {
Bar string `json:"bar"`
}

type FooBuzz[R Custom] struct {
Something []R `json:"something"`
}

type Custom interface {
Foo | Buzz
}

// Not yet supported
//type FooBuzzMap[R Custom] struct {
//Something map[string]R `json:"something"`
Expand Down
4 changes: 2 additions & 2 deletionsscripts/apitypings/testdata/genericmap/genericmap.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,8 +11,8 @@ export interface Foo {

// From codersdk/genericmap.go
export interface FooBuzz<R extends Custom> {
readonly something: R[]
readonly something:(readonlyR[])
}

// From codersdk/genericmap.go
export type Custom = Foo | Buzz
export type Custom = Foo | Buzz
4 changes: 2 additions & 2 deletionsscripts/apitypings/testdata/generics/generics.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,9 +33,9 @@ export interface Static {
}

// From codersdk/generics.go
export type Custom = string | boolean | number | string[] | null
export type Custom = string | boolean | number |(readonlystring[]) | null

// From codersdk/generics.go
export type Single = string

export type comparable = boolean | number | string | any
export type comparable = boolean | number | string | any
10 changes: 10 additions & 0 deletionsscripts/apitypings/testdata/genericslice/genericslice.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
package codersdk

type Bar struct {
Bar string
}

type Foo[R any] struct {
Slice []R
TwoD [][]R
}
10 changes: 10 additions & 0 deletionsscripts/apitypings/testdata/genericslice/genericslice.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
// From codersdk/genericslice.go
export interface Bar {
readonly Bar: string
}

// From codersdk/genericslice.go
export interface Foo<R extends any> {
Copy link
Member

@ParkreinerParkreinerApr 15, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Not for the future:R extends any actually doesn't do anything at all. Sinceany is a top type, everything in TypeScript extendsany, so the type constraint provides zero protection

At some point, we'd probably want to turn this into

exportinterfaceFoo<R=unknown>{// Stuff}

readonly Slice: (readonly R[])
readonly TwoD: (readonly (readonly R[])[])
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp