cuetsy
cuetsy copied to clipboard
Experimental CUE->TypeScript exporter
Installation · Usage
cuetsy
Converting CUE objects to their TypeScript equivalent (highly experimental!)
- CUE makes defining and validating canonical data specification easy
- TypeScript is dominant in the frontend, but cannot natively benefit from this
- CUE types often have direct TypeScript equivalents, so cuetsy can bridge this gap
Example
| CUE | TypeScript |
|---|---|
|
|
Status
Cuetsy is experimental. The following are supported:
- Types
- Unions
- Interfaces
- Enums
- Default
const
Installation
Cuetsy can be installed using Go 1.16+
$ go install github.com/grafana/cuetsy/cmd/cuetsy
Usage
cuetsy must be invoked on files as follows:
$ cuetsy [file.cue]
This will create a logically equivalent [file].ts
Alternatively, cuetsy can be used as a library for more customized code generation.
Union Types
| CUE | TypeScript | @cuetsy(kind) |
|---|---|---|
| Disjunction | Union Type | type |
Union types are expressed in CUE and TypeScript nearly the same way, namely a
series of disjunctions (a | b | c):
| CUE | TypeScript |
|---|---|
|
|
Interfaces
| CUE | TypeScript | @cuetsy(kind) |
|---|---|---|
| Struct | Interface | interface |
TypeScript interfaces are expressed as regular structs in CUE.
Caveats:
- Default generation does not work correctly for optional nested structs. Issue
| CUE | TypeScript |
|---|---|
|
|
Inheritance
Interfaces can optionally extend another interface. If a type marked for
export as a kind="interface" is unified (whether by & or embedding) with
another type marked for export as an interface, it will produce extend in output:
| CUE | TypeScript |
|---|---|
|
|
Enums
| CUE | TypeScript | @cuetsy(kind) |
|---|---|---|
| Disjunction | String enums, Numeric enums | enum |
TypeScript's enums are union types, and are a mostly-exact mapping of what can be expressed with CUE's disjunctions. Disjunctions may contain only string or numeric values.
For string enums, the member names (keys) of the TypeScript enum are automatically inferred as the
titled camel-case variant of their string value, but may be explicitly specified
using the memberNames attribute. For a numeric enum, memberNames must be specified.
| CUE | TypeScript |
|---|---|
|
|
Defaults
| CUE | TypeScript |
|---|---|
| Defaults | const |
Cuetsy can optionally generate a const for each type that holds default
values. For that, mark CUE Default
Values to your type
definitions:
| CUE | TypeScript |
|---|---|
|
|