typescript

package
v0.0.0-...-dbdf344 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package typescript provides primitives to represent TypeScript types, and type declarations.

This package is completely decoupled from Go's reflect package. It provides a set of very simple primitives to build an AST-like representation of TypeScript types, and type declarations. Each primitive includes a ToTypeScript() method that can be used to recursively generate Go2TS's output TypeScript code.

These primitives decouple Go2TS's reflection code from its code generation code, which makes Go2TS easier to debug and reason about, and provide a flexible foundation that will allow us to extend Go2TS in the future with support for additional types, new features, etc.

The "root" type in this package is the TypeDeclaration interface, which is implemented by the InterfaceDeclaration and TypeAliasDeclaration structs.

The names of the primitives in this package are loosely based on the names and terms used in the TypeScript AST. Recommended links:

Index

Constants

View Source
const (
	// Boolean represents the "boolean" TypeScript type.
	Boolean = BasicType("boolean")

	// Number represents the "number" TypeScript type.
	Number = BasicType("number")

	// String represents the "string" TypeScript type.
	String = BasicType("string")

	// Null represents the "null" TypeScript type.
	Null = BasicType("null")

	// Any represents the "null" TypeScript type.
	Any = BasicType("any")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayType

type ArrayType struct {
	ItemsType Type
}

ArrayType represents a TypeScript array type such as string[], MyType[], etc.

func (*ArrayType) ToTypeScript

func (a *ArrayType) ToTypeScript() string

ToTypeScript implements the Type interface.

type BasicType

type BasicType string

BasicType represents a TypeScript basic type supported by Go2TS.

The "null" and "any" types are represented as basic types for simplicity.

func (BasicType) ToTypeScript

func (b BasicType) ToTypeScript() string

ToTypeScript implements the Type interface.

type InterfaceDeclaration

type InterfaceDeclaration struct {
	// Namespace is the namespace that the interface belongs to, or empty for the global namespace.
	Namespace  string
	Identifier string
	Properties []PropertySignature
}

InterfaceDeclaration represents a TypeScript interface declaration.

func (*InterfaceDeclaration) QualifiedName

func (i *InterfaceDeclaration) QualifiedName() string

QualifiedName implements the TypeDeclaration interface.

func (*InterfaceDeclaration) ToTypeScript

func (i *InterfaceDeclaration) ToTypeScript() string

ToTypeScript implements the TypeDeclaration interface.

func (*InterfaceDeclaration) TypeReference

func (i *InterfaceDeclaration) TypeReference() *TypeReference

TypeReference implements the TypeDeclaration interface.

type LiteralType

type LiteralType struct {
	BasicType BasicType
	Literal   string
}

LiteralType represents a TypeScript literal type such as "hello", 123, true, etc.

See https://www.typescriptlang.org/docs/handbook/literal-types.html.

func (*LiteralType) ToTypeScript

func (l *LiteralType) ToTypeScript() string

ToTypeScript implements the Type interface.

type MapType

type MapType struct {
	IndexType Type
	ValueType Type
}

MapType represents a TypeScript type that describes an object used as a dictionary of key/value pairs, e.g. { [key: string]: MyStruct }.

func (*MapType) ToTypeScript

func (m *MapType) ToTypeScript() string

ToTypeScript implements the Type interface.

type PropertySignature

type PropertySignature struct {
	Identifier string
	Type       Type
	Optional   bool
}

PropertySignature represents a property signature of a TypeScript interface declaration.

func (*PropertySignature) ToTypeScript

func (p *PropertySignature) ToTypeScript() string

ToTypeScript converts the PropertySignature to a valid TypeScript interface property declaration.

type Type

type Type interface {
	// ToTypeScript returns a TypeScript expression that can be used as a type, or panics if the type
	// is invalid.
	ToTypeScript() string
	// contains filtered or unexported methods
}

Type represents a TypeScript type.

type TypeAliasDeclaration

type TypeAliasDeclaration struct {
	// Namespace is the namespace that the type alias belongs to, or empty for the global namespace.
	Namespace  string
	Identifier string
	Type       Type
	// GenerateNominalTypes tells TypeAliasDeclaration to generate nominal TypeScript types
	// for aliased Go types. For background on "nominal typing" in TypeScript, see
	// https://www.typescriptlang.org/play#example/nominal-typing and
	// https://basarat.gitbook.io/typescript/main-1/nominaltyping.
	// The TypeScript compiler itself uses this technique to achieve
	// a similar effect as with Go's type aliases:
	// https://github.com/Microsoft/TypeScript/blob/7b48a182c05ea4dea81bab73ecbbe9e013a79e99/src/compiler/types.ts#L693
	GenerateNominalTypes bool
}

TypeAliasDeclaration represents a TypeScript type alias declaration, e.g. type Color = string.

func (*TypeAliasDeclaration) QualifiedName

func (a *TypeAliasDeclaration) QualifiedName() string

QualifiedName implements the TypeDeclaration interface.

func (*TypeAliasDeclaration) ToTypeScript

func (a *TypeAliasDeclaration) ToTypeScript() string

ToTypeScript implements the TypeDeclaration interface.

func (*TypeAliasDeclaration) TypeReference

func (a *TypeAliasDeclaration) TypeReference() *TypeReference

TypeReference implements the TypeDeclaration interface.

type TypeDeclaration

type TypeDeclaration interface {
	// TypeReference returns a reference to the declared type.
	TypeReference() *TypeReference

	// QualifiedName returns the qualified name of the declared type, e.g. MyNamespace.MyType.
	QualifiedName() string

	// ToTypeScript converts the TypeDeclaration to valid TypeScript.
	ToTypeScript() string
	// contains filtered or unexported methods
}

TypeDeclaration represents a TypeScript type declaration, which can be an interface declaration, a type alias, etc.

type TypeReference

type TypeReference struct {
	// contains filtered or unexported fields
}

TypeReference represents a reference to a type declared via a TypeDeclaration (e.g. an interface, a type alias, etc.) and can be used anywhere a Type can be used.

func (*TypeReference) ToTypeScript

func (t *TypeReference) ToTypeScript() string

ToTypeScript implements the Type interface.

type UnionType

type UnionType struct {
	Types []Type
}

UnionType represents a TypeScript union type, e.g. 'up' | 'right' | 'down' | 'left'.

func (UnionType) ToTypeScript

func (u UnionType) ToTypeScript() string

ToTypeScript implements the Type interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL