types

package
v0.9.12 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Number    = &Primitive{Name: "number"}
	String    = &Primitive{Name: "string"}
	Boolean   = &Primitive{Name: "boolean"}
	Symbol    = &Primitive{Name: "symbol"}
	BigInt    = &Primitive{Name: "bigint"}
	Null      = &Primitive{Name: "null"}
	Undefined = &Primitive{Name: "undefined"}
	Any       = &Primitive{Name: "any"}
	Unknown   = &Primitive{Name: "unknown"}
	Never     = &Primitive{Name: "never"}
	Void      = &Primitive{Name: "void"}
	RegExp    = &Primitive{Name: "RegExp"}
)

Pre-defined instances for common primitive types

View Source
var TypeofResultType = NewUnionType(
	&LiteralType{Value: vm.String("undefined")},
	&LiteralType{Value: vm.String("boolean")},
	&LiteralType{Value: vm.String("number")},
	&LiteralType{Value: vm.String("bigint")},
	&LiteralType{Value: vm.String("string")},
	&LiteralType{Value: vm.String("function")},
	&LiteralType{Value: vm.String("object")},
)

TypeofResultType represents the union of all possible string literals that the typeof operator can return

Functions

func GetEnumMemberValue

func GetEnumMemberValue(t Type) (interface{}, bool)

GetEnumMemberValue returns the value of an enum member if it's an enum member type

func IsAssignable

func IsAssignable(source, target Type) bool

func IsEnumMemberType

func IsEnumMemberType(t Type) bool

IsEnumMemberType checks if a type is an EnumMemberType

func IsEnumType

func IsEnumType(t Type) bool

IsEnumType checks if a type is an EnumType

func IsLiteral

func IsLiteral(t Type) bool

IsLiteral returns true if the type is a literal type

func IsNullOrUndefined

func IsNullOrUndefined(t Type) bool

IsNullOrUndefined returns true if the type is null or undefined

func IsPrimitive

func IsPrimitive(t Type) bool

IsPrimitive returns true if the type is a primitive type

func IsReadonlyType

func IsReadonlyType(t Type) bool

IsReadonlyType checks if a type is a readonly type

func ObjectTypeIsCallable

func ObjectTypeIsCallable(objType *ObjectType) bool

ObjectTypeIsCallable checks if an object type has call signatures

func SetPrototypeMethodResolver

func SetPrototypeMethodResolver(resolver GetMethodType)

SetPrototypeMethodResolver sets the resolver for prototype methods This should be called by the checker package during initialization

Types

type AccessContext

type AccessContext struct {
	// Name of the class currently being checked
	CurrentClassName string

	// Type of access context
	ContextType AccessContextType

	// Whether we're inside a constructor
	IsInConstructor bool

	// Whether we're in a static context
	IsStaticContext bool

	// Function to check inheritance relationships
	// Returns true if currentClass is a subclass of targetClass
	IsSubclassOfFunc func(currentClass, targetClass string) bool
}

AccessContext represents the context from which a member is being accessed

func NewAccessContext

func NewAccessContext(className string, contextType AccessContextType) *AccessContext

NewAccessContext creates a new access context

func (*AccessContext) IsSubclassOf

func (ac *AccessContext) IsSubclassOf(targetClass string) bool

IsSubclassOf checks if the current class is a subclass of the target class

type AccessContextType

type AccessContextType int

AccessContextType represents where the access is happening from

const (
	AccessContextExternal AccessContextType = iota
	AccessContextInstanceMethod
	AccessContextStaticMethod
	AccessContextConstructor
)

func (AccessContextType) String

func (act AccessContextType) String() string

String returns the string representation of the access context type

type AccessModifier

type AccessModifier int

AccessModifier represents the visibility level of class members

const (
	AccessPublic AccessModifier = iota
	AccessPrivate
	AccessProtected
)

func (AccessModifier) String

func (a AccessModifier) String() string

String returns the string representation of the access modifier

type AliasType

type AliasType struct {
	Name         string
	ResolvedType Type // The actual type this alias points to after resolution
}

AliasType represents a named type alias.

func (*AliasType) Equals

func (at *AliasType) Equals(other Type) bool

func (*AliasType) String

func (at *AliasType) String() string

type ArrayType

type ArrayType struct {
	ElementType Type
}

ArrayType represents the type of an array.

func (*ArrayType) Equals

func (at *ArrayType) Equals(other Type) bool

func (*ArrayType) String

func (at *ArrayType) String() string

type ClassMetadata

type ClassMetadata struct {
	// Name of the class this type represents
	ClassName string

	// Access control information for each member
	MemberAccess map[string]*MemberAccessInfo

	// Indicates this is a class instance type (not the constructor)
	IsClassInstance bool

	// Indicates this is a class constructor type
	IsClassConstructor bool

	// Reference to the source class declaration (if available)
	// This is used for inheritance checks and access validation
	SourceClassName string

	// Inheritance relationships
	SuperClassName        string   // The class this class extends (if any)
	SuperConstructorType  Type     // The resolved constructor type of the superclass (if any)
	ImplementedInterfaces []string // The interfaces this class implements
}

ClassMetadata contains class-specific type information for access control

func NewClassMetadata

func NewClassMetadata(className string, isInstance bool) *ClassMetadata

NewClassMetadata creates a new ClassMetadata instance

func (*ClassMetadata) AddGetterMember

func (cm *ClassMetadata) AddGetterMember(memberName string, accessLevel AccessModifier, isStatic bool)

AddGetterMember adds a getter method with access control information

func (*ClassMetadata) AddImplementedInterface

func (cm *ClassMetadata) AddImplementedInterface(interfaceName string)

AddImplementedInterface adds an interface that this class implements

func (*ClassMetadata) AddMember

func (cm *ClassMetadata) AddMember(memberName string, accessLevel AccessModifier, isStatic, isReadonly bool)

AddMember adds access control information for a class member

func (*ClassMetadata) AddSetterMember

func (cm *ClassMetadata) AddSetterMember(memberName string, accessLevel AccessModifier, isStatic bool)

AddSetterMember adds a setter method with access control information

func (*ClassMetadata) ExtendsClass

func (cm *ClassMetadata) ExtendsClass(className string) bool

ExtendsClass returns true if this class extends the given class name

func (*ClassMetadata) GetMemberAccess

func (cm *ClassMetadata) GetMemberAccess(memberName string) *MemberAccessInfo

GetMemberAccess returns the access information for a member, or nil if not found

func (*ClassMetadata) HasMember

func (cm *ClassMetadata) HasMember(memberName string) bool

HasMember checks if a member exists in this class

func (*ClassMetadata) ImplementsInterface

func (cm *ClassMetadata) ImplementsInterface(interfaceName string) bool

ImplementsInterface returns true if this class implements the given interface

func (*ClassMetadata) IsAccessibleFrom

func (cm *ClassMetadata) IsAccessibleFrom(memberName string, accessContext *AccessContext) bool

IsAccessibleFrom checks if a member is accessible from a given class context

func (*ClassMetadata) IsSubclassOf

func (cm *ClassMetadata) IsSubclassOf(targetClass string, getClassMeta func(string) *ClassMetadata) bool

IsSubclassOf returns true if this class is a subclass of the given class name This checks the entire inheritance chain, not just direct inheritance

func (*ClassMetadata) SetSuperClass

func (cm *ClassMetadata) SetSuperClass(superClassName string)

SetSuperClass sets the superclass for this class

func (*ClassMetadata) SetSuperClassWithConstructor

func (cm *ClassMetadata) SetSuperClassWithConstructor(superClassName string, constructorType Type)

SetSuperClassWithConstructor sets both the superclass name and its resolved constructor type

type ClassType

type ClassType struct {
	Name            string      // The class name (e.g., "Animal")
	ConstructorType *ObjectType // Constructor signature as ObjectType with construct signature
	InstanceType    *ObjectType // Shape of instances created by this class
	StaticType      *ObjectType // Static methods and properties (for future use)
	SuperClass      *ClassType  // Parent class for inheritance (for future use)
}

ClassType represents a TypeScript/JavaScript class type

func NewClassType

func NewClassType(name string, constructorSig *Signature, instanceType *ObjectType) *ClassType

NewClassType creates a new class type with the given constructor and instance types

func NewSimpleClassType

func NewSimpleClassType(name string, paramTypes []Type, instanceType *ObjectType) *ClassType

NewSimpleClassType creates a class type with a simple constructor signature

func (*ClassType) Equals

func (ct *ClassType) Equals(other Type) bool

func (*ClassType) GetConstructorSignature

func (ct *ClassType) GetConstructorSignature() *Signature

GetConstructorSignature returns the constructor signature for this class

func (*ClassType) IsInstanceOf

func (ct *ClassType) IsInstanceOf(instanceType Type) bool

IsInstanceOf checks if a given type could be an instance of this class

func (*ClassType) String

func (ct *ClassType) String() string

func (*ClassType) WithMethod

func (ct *ClassType) WithMethod(name string, methodType Type) *ClassType

WithMethod adds a method to the instance type of this class

func (*ClassType) WithProperty

func (ct *ClassType) WithProperty(name string, propType Type) *ClassType

WithProperty adds a property to the instance type of this class

type ConditionalType

type ConditionalType struct {
	CheckType   Type // The type being checked (T in T extends U ? X : Y)
	ExtendsType Type // The type being extended/checked against (U in T extends U ? X : Y)
	TrueType    Type // The type when condition is true (X in T extends U ? X : Y)
	FalseType   Type // The type when condition is false (Y in T extends U ? X : Y)
}

ConditionalType represents a conditional type: CheckType extends ExtendsType ? TrueType : FalseType

func (*ConditionalType) Equals

func (ct *ConditionalType) Equals(other Type) bool

func (*ConditionalType) String

func (ct *ConditionalType) String() string

type EnumMemberType

type EnumMemberType struct {
	EnumName   string      // Parent enum name
	MemberName string      // Member name
	Value      interface{} // Runtime value (int or string)
}

EnumMemberType represents a specific enum member literal type (e.g., Color.Red)

func (*EnumMemberType) Equals

func (em *EnumMemberType) Equals(other Type) bool

Equals checks if this enum member type equals another type

func (*EnumMemberType) GetName

func (em *EnumMemberType) GetName() string

GetName returns the member name (for consistency with other types)

func (*EnumMemberType) String

func (em *EnumMemberType) String() string

String returns the string representation of the enum member type

func (*EnumMemberType) TypeString

func (em *EnumMemberType) TypeString() string

TypeString returns the type string representation

type EnumType

type EnumType struct {
	Name      string
	Members   map[string]*EnumMemberType // Map of member name to member type
	IsConst   bool                       // True for const enums
	IsNumeric bool                       // True if all members are numeric
}

EnumType represents an enum type (e.g., Color with members Red, Green, Blue)

func (*EnumType) Equals

func (e *EnumType) Equals(other Type) bool

Equals checks if this enum type equals another type

func (*EnumType) GetName

func (e *EnumType) GetName() string

GetName returns the enum name

func (*EnumType) String

func (e *EnumType) String() string

String returns the string representation of the enum type

func (*EnumType) TypeString

func (e *EnumType) TypeString() string

TypeString returns the type string representation

type ForwardReferenceType

type ForwardReferenceType struct {
	ClassName      string
	TypeParameters []*TypeParameter
}

ForwardReferenceType represents a forward reference to a generic class being defined

func (*ForwardReferenceType) Equals

func (frt *ForwardReferenceType) Equals(other Type) bool

func (*ForwardReferenceType) String

func (frt *ForwardReferenceType) String() string

type GenericType

type GenericType struct {
	Name           string           // Name of the generic type (e.g., "Array", "Promise")
	TypeParameters []*TypeParameter // The type parameters (e.g., [T] for Array<T>)
	Body           Type             // The body type that may contain TypeParameterType references
}

GenericType represents a generic type definition (before instantiation) This is the "template" that gets instantiated with concrete types

var (
	// Array<T> generic type
	ArrayGeneric *GenericType

	// Promise<T> generic type
	PromiseGeneric *GenericType

	// Generator<T, TReturn, TNext> generic type
	GeneratorGeneric *GenericType

	// AsyncGenerator<T, TReturn, TNext> generic type
	AsyncGeneratorGeneric *GenericType
)

func NewGenericType

func NewGenericType(name string, typeParams []*TypeParameter, body Type) *GenericType

NewGenericType creates a new generic type definition

func (*GenericType) Equals

func (g *GenericType) Equals(other Type) bool

func (*GenericType) String

func (g *GenericType) String() string

type GenericTypeAliasForwardReference

type GenericTypeAliasForwardReference struct {
	AliasName     string
	TypeArguments []Type
}

GenericTypeAliasForwardReference represents a forward reference to a generic type alias being defined

func (*GenericTypeAliasForwardReference) Equals

func (gtafr *GenericTypeAliasForwardReference) Equals(other Type) bool

func (*GenericTypeAliasForwardReference) String

func (gtafr *GenericTypeAliasForwardReference) String() string

type GetMethodType

type GetMethodType func(objectType string, methodName string) Type

GetMethodType is a function type for resolving prototype methods This type is used to decouple the types package from builtins

type IndexSignature

type IndexSignature struct {
	KeyType   Type // The type of the key (e.g., string, number, symbol)
	ValueType Type // The type of the value

	// For mapped types: [P in K]: V
	IsMapped       bool   // Whether this is a mapped type pattern
	TypeParameter  string // The type parameter name (e.g., "P" in [P in K])
	ConstraintType Type   // The constraint type (e.g., K in [P in K])
}

IndexSignature represents an index signature like [key: string]: Type or a mapped type pattern like [P in K]: V

func (*IndexSignature) Equals

func (is *IndexSignature) Equals(other *IndexSignature) bool

func (*IndexSignature) String

func (is *IndexSignature) String() string

type IndexedAccessType

type IndexedAccessType struct {
	ObjectType Type // The type we're indexing into (e.g., T in T[K])
	IndexType  Type // The key type used for indexing (e.g., K in T[K])
}

IndexedAccessType represents an indexed access type like T[K] This is used to access properties of a type using a key type

func (*IndexedAccessType) Equals

func (iat *IndexedAccessType) Equals(other Type) bool

func (*IndexedAccessType) String

func (iat *IndexedAccessType) String() string

type InferType

type InferType struct {
	TypeParameter string // The type parameter being inferred (e.g., 'R' in 'infer R')
}

InferType represents an infer type in conditional types like infer R This is used to capture and infer types during conditional type resolution

func (*InferType) Equals

func (it *InferType) Equals(other Type) bool

func (*InferType) String

func (it *InferType) String() string

type InstantiatedType

type InstantiatedType struct {
	Generic       *GenericType // The generic type being instantiated
	TypeArguments []Type       // The concrete type arguments (e.g., [string] for Array<string>)
	// contains filtered or unexported fields
}

InstantiatedType represents a generic type with concrete type arguments This is what you get when you write Array<string> - an instantiation of the Array generic

func NewInstantiatedType

func NewInstantiatedType(generic *GenericType, typeArgs []Type) *InstantiatedType

NewInstantiatedType creates a new instantiated generic type

func (*InstantiatedType) Equals

func (i *InstantiatedType) Equals(other Type) bool

func (*InstantiatedType) String

func (i *InstantiatedType) String() string

func (*InstantiatedType) Substitute

func (i *InstantiatedType) Substitute() Type

Substitute returns the concrete type by replacing type parameters with type arguments This is where the "type-level lambda" application happens

type IntersectionType

type IntersectionType struct {
	Types []Type // Slice holding the types in the intersection
}

IntersectionType represents an intersection of multiple types (e.g., A & B). A value of intersection type must satisfy ALL constituent types simultaneously.

func (*IntersectionType) Equals

func (it *IntersectionType) Equals(other Type) bool

func (*IntersectionType) String

func (it *IntersectionType) String() string

type KeyofType

type KeyofType struct {
	OperandType Type // The type we're getting keys from
}

KeyofType represents a keyof type operator like keyof T This evaluates to a union of string literal types representing the keys of the operand type

func (*KeyofType) Equals

func (kt *KeyofType) Equals(other Type) bool

func (*KeyofType) String

func (kt *KeyofType) String() string

type LiteralType

type LiteralType struct {
	Value vm.Value // Holds the literal value (e.g., vm.Number(5), vm.String("hello"))
}

LiteralType represents a specific literal value used as a type.

func (*LiteralType) Equals

func (lt *LiteralType) Equals(other Type) bool

func (*LiteralType) Name

func (lt *LiteralType) Name() string

func (*LiteralType) String

func (lt *LiteralType) String() string

type MappedType

type MappedType struct {
	TypeParameter  string // The iteration variable (e.g., "P" in [P in K])
	ConstraintType Type   // The type being iterated over (e.g., K in [P in K])
	ValueType      Type   // The resulting value type for each property

	// Modifiers for the mapped type
	ReadonlyModifier string // "+", "-", or "" (for readonly modifier)
	OptionalModifier string // "+", "-", or "" (for optional modifier)
}

MappedType represents a mapped type like { [P in K]: T } This is used for utility types like Partial<T>, Readonly<T>, etc.

func (*MappedType) Equals

func (mt *MappedType) Equals(other Type) bool

func (*MappedType) String

func (mt *MappedType) String() string

type MemberAccessInfo

type MemberAccessInfo struct {
	AccessLevel AccessModifier
	IsStatic    bool
	IsReadonly  bool
	IsGetter    bool // This property is defined with 'get' keyword
	IsSetter    bool // This property is defined with 'set' keyword
}

MemberAccessInfo contains access control information for a class member

type ObjectType

type ObjectType struct {
	// Using a map for simplicity now. Consider ordered map or slice for stability.
	Properties         map[string]Type
	OptionalProperties map[string]bool // Tracks which properties are optional
	ReadOnlyProperties map[string]bool // Tracks which properties are readonly

	// NEW: Unified callable/constructor support
	CallSignatures      []*Signature // Object call signatures: obj(args)
	ConstructSignatures []*Signature // Object constructor signatures: new obj(args)
	BaseTypes           []Type       // For inheritance (classes, interfaces)

	// NEW: Class metadata for access control
	ClassMeta *ClassMetadata // Contains access control information for class types

	// Index signatures for dynamic property access
	IndexSignatures []*IndexSignature // Index signatures like [key: string]: Type

	// IsReflectIntrinsic marks this as a compile-time type reflection intrinsic
	// When the checker sees a call to a function with this flag, it resolves the type argument
	// and stores it for the compiler to emit a type descriptor object
	IsReflectIntrinsic bool
}

ObjectType represents the type of an object literal or interface.

func MergeObjectTypes

func MergeObjectTypes(objectTypes []*ObjectType) *ObjectType

MergeObjectTypes merges multiple object types into a single object type. This handles property conflicts and optional properties.

func NewClassConstructorType

func NewClassConstructorType(className string, sig *Signature) *ObjectType

NewClassConstructorType creates an ObjectType representing a class constructor

func NewClassInstanceType

func NewClassInstanceType(className string) *ObjectType

NewClassInstanceType creates an ObjectType representing a class instance

func NewConstructorType

func NewConstructorType(sig *Signature) *ObjectType

NewConstructorType creates an ObjectType representing a pure constructor

func NewFunctionType

func NewFunctionType(sig *Signature) *ObjectType

NewFunctionType creates an ObjectType representing a pure function

func NewObjectType

func NewObjectType() *ObjectType

NewObjectType creates a new ObjectType with empty properties and signatures

func NewOptionalFunction

func NewOptionalFunction(paramTypes []Type, returnType Type, optionalParams []bool) *ObjectType

NewOptionalFunction creates a function type with optional parameters: (param1, param2?) => returnType

func NewOverloadedFunctionType

func NewOverloadedFunctionType(sigs []*Signature) *ObjectType

NewOverloadedFunctionType creates an ObjectType representing an overloaded function

func NewSimpleConstructor

func NewSimpleConstructor(paramTypes []Type, returnType Type) *ObjectType

NewSimpleConstructor creates a constructor type: new (params) => returnType

func NewSimpleFunction

func NewSimpleFunction(paramTypes []Type, returnType Type) *ObjectType

NewSimpleFunction creates a function type: (params) => returnType

func NewVariadicFunction

func NewVariadicFunction(paramTypes []Type, returnType Type, restType Type) *ObjectType

NewVariadicFunction creates a variadic function type: (params, ...rest) => returnType

func (*ObjectType) AddBaseType

func (ot *ObjectType) AddBaseType(baseType Type)

AddBaseType adds a base type for inheritance

func (*ObjectType) AddCallSignature

func (ot *ObjectType) AddCallSignature(sig *Signature)

AddCallSignature adds a call signature to this ObjectType

func (*ObjectType) AddConstructSignature

func (ot *ObjectType) AddConstructSignature(sig *Signature)

AddConstructSignature adds a constructor signature to this ObjectType

func (*ObjectType) AsClassConstructor

func (ot *ObjectType) AsClassConstructor(className string) *ObjectType

AsClassConstructor sets this ObjectType as a class constructor type with the given class name

func (*ObjectType) AsClassInstance

func (ot *ObjectType) AsClassInstance(className string) *ObjectType

AsClassInstance sets this ObjectType as a class instance type with the given class name

func (*ObjectType) Equals

func (ot *ObjectType) Equals(other Type) bool

func (*ObjectType) GetCallSignatures

func (ot *ObjectType) GetCallSignatures() []*Signature

GetCallSignatures returns the call signatures of this ObjectType

func (*ObjectType) GetClassName

func (ot *ObjectType) GetClassName() string

GetClassName returns the class name if this is a class type, empty string otherwise

func (*ObjectType) GetConstructSignatures

func (ot *ObjectType) GetConstructSignatures() []*Signature

GetConstructSignatures returns the constructor signatures of this ObjectType

func (*ObjectType) GetEffectiveProperties

func (ot *ObjectType) GetEffectiveProperties() map[string]Type

GetEffectiveProperties returns all properties including inherited ones from base types

func (*ObjectType) GetMemberAccessInfo

func (ot *ObjectType) GetMemberAccessInfo(memberName string) *MemberAccessInfo

GetMemberAccessInfo returns access information for a member, or nil if not a class type

func (*ObjectType) Inherits

func (ot *ObjectType) Inherits(baseType Type) *ObjectType

Inherits adds a base type for inheritance and returns the same instance for chaining

func (*ObjectType) IsAccessibleFrom

func (ot *ObjectType) IsAccessibleFrom(memberName string, accessContext *AccessContext) bool

IsAccessibleFrom checks if a member is accessible from the given context

func (*ObjectType) IsCallable

func (ot *ObjectType) IsCallable() bool

IsCallable returns true if this ObjectType has call signatures

func (*ObjectType) IsClassConstructor

func (ot *ObjectType) IsClassConstructor() bool

IsClassConstructor returns true if this ObjectType represents a class constructor

func (*ObjectType) IsClassInstance

func (ot *ObjectType) IsClassInstance() bool

IsClassInstance returns true if this ObjectType represents a class instance

func (*ObjectType) IsConstructable

func (ot *ObjectType) IsConstructable() bool

IsConstructable returns true if this ObjectType has constructor signatures

func (*ObjectType) IsPropertyOptional added in v0.9.11

func (ot *ObjectType) IsPropertyOptional(name string) bool

IsPropertyOptional checks whether a property is optional, including inherited properties

func (*ObjectType) IsPureFunction

func (ot *ObjectType) IsPureFunction() bool

IsPureFunction returns true if this ObjectType is callable but has no properties (i.e., it's a pure function)

func (*ObjectType) IsReadOnly

func (ot *ObjectType) IsReadOnly(name string) bool

IsReadOnly returns whether a property is readonly

func (*ObjectType) String

func (ot *ObjectType) String() string

func (*ObjectType) WithCallSignature

func (ot *ObjectType) WithCallSignature(sig *Signature) *ObjectType

WithCallSignature adds a call signature to the ObjectType and returns the same instance for chaining

func (*ObjectType) WithClassMember

func (ot *ObjectType) WithClassMember(memberName string, memberType Type, accessLevel AccessModifier, isStatic, isReadonly bool) *ObjectType

WithClassMember adds a class member with access control information

func (*ObjectType) WithConstructSignature

func (ot *ObjectType) WithConstructSignature(sig *Signature) *ObjectType

WithConstructSignature adds a constructor signature to the ObjectType and returns the same instance for chaining

func (*ObjectType) WithOptionalProperty

func (ot *ObjectType) WithOptionalProperty(name string, propType Type) *ObjectType

WithOptionalProperty adds an optional property to the ObjectType and returns the same instance for chaining

func (*ObjectType) WithProperty

func (ot *ObjectType) WithProperty(name string, propType Type) *ObjectType

WithProperty adds a required property to the ObjectType and returns the same instance for chaining

func (*ObjectType) WithReadOnlyProperty

func (ot *ObjectType) WithReadOnlyProperty(name string, propType Type) *ObjectType

WithReadOnlyProperty adds a readonly property to the ObjectType and returns the same instance for chaining

func (*ObjectType) WithSimpleCallSignature

func (ot *ObjectType) WithSimpleCallSignature(paramTypes []Type, returnType Type) *ObjectType

WithSimpleCallSignature adds a simple call signature (params->return) and returns the same instance for chaining

func (*ObjectType) WithSimpleConstructSignature

func (ot *ObjectType) WithSimpleConstructSignature(paramTypes []Type, returnType Type) *ObjectType

WithSimpleConstructSignature adds a simple constructor signature and returns the same instance for chaining

func (*ObjectType) WithVariadicCallSignature

func (ot *ObjectType) WithVariadicCallSignature(paramTypes []Type, returnType Type, restType Type) *ObjectType

WithVariadicCallSignature adds a variadic call signature and returns the same instance for chaining

func (*ObjectType) WithVariadicProperty

func (ot *ObjectType) WithVariadicProperty(name string, paramTypes []Type, returnType Type, restType Type) *ObjectType

WithVariadicProperty adds a variadic method property to the ObjectType and returns the same instance for chaining

type ObjectTypeMarker

type ObjectTypeMarker struct{}

ObjectTypeMarker is used in type narrowing to indicate "typeof x === 'object'" constraints This filters union types to only object-like types (objects, arrays, but not primitives)

func (*ObjectTypeMarker) Equals

func (o *ObjectTypeMarker) Equals(other Type) bool

func (*ObjectTypeMarker) String

func (o *ObjectTypeMarker) String() string

type ParameterizedForwardReferenceType

type ParameterizedForwardReferenceType struct {
	ClassName     string
	TypeArguments []Type
}

ParameterizedForwardReferenceType represents a forward reference to a generic class with type arguments For example, Node<T> when used inside the Node<T> class definition

func (*ParameterizedForwardReferenceType) Equals

func (pfrt *ParameterizedForwardReferenceType) Equals(other Type) bool

func (*ParameterizedForwardReferenceType) String

type Primitive

type Primitive struct {
	Name string
}

Primitive represents a fundamental, non-composite type.

func (*Primitive) Equals

func (p *Primitive) Equals(other Type) bool

func (*Primitive) String

func (p *Primitive) String() string

type PropertyExistenceMarker

type PropertyExistenceMarker struct {
	PropertyName string
}

PropertyExistenceMarker is used in type narrowing for "prop in obj" checks This filters union types to only types that have the specified property

func (*PropertyExistenceMarker) Equals

func (p *PropertyExistenceMarker) Equals(other Type) bool

func (*PropertyExistenceMarker) String

func (p *PropertyExistenceMarker) String() string

type ReadonlyType

type ReadonlyType struct {
	InnerType Type // The wrapped type that becomes readonly
}

ReadonlyType represents a readonly wrapper around another type This allows `readonly foo: number` to unify with `foo: Readonly<number>`

func NewReadonlyType

func NewReadonlyType(innerType Type) *ReadonlyType

NewReadonlyType creates a new readonly wrapper around a type

func (*ReadonlyType) Equals

func (r *ReadonlyType) Equals(other Type) bool

func (*ReadonlyType) String

func (r *ReadonlyType) String() string

type Signature

type Signature struct {
	ParameterTypes    []Type
	ReturnType        Type
	OptionalParams    []bool // Tracks which parameters are optional
	IsVariadic        bool   // Indicates if the function accepts variable arguments
	RestParameterType Type   // Type of the rest parameter (...args), if present
}

Signature represents a function or constructor signature

func NewSignature

func NewSignature(paramTypes ...Type) *Signature

NewSignature creates a new signature builder with the given parameter types

func Sig

func Sig(paramTypes []Type, returnType Type) *Signature

Sig creates a Signature with the given parameters and return type

func SigOptional

func SigOptional(paramTypes []Type, returnType Type, optionalParams []bool) *Signature

SigOptional creates a Signature with optional parameters

func SigVariadic

func SigVariadic(paramTypes []Type, returnType Type, restType Type) *Signature

SigVariadic creates a variadic Signature

func (*Signature) Equals

func (sig *Signature) Equals(other *Signature) bool

func (*Signature) Returns

func (sig *Signature) Returns(returnType Type) *Signature

Returns sets the return type (fluent interface)

func (*Signature) String

func (sig *Signature) String() string

func (*Signature) ToFunction

func (sig *Signature) ToFunction() *ObjectType

ToFunction wraps this signature in an ObjectType with a single call signature

func (*Signature) WithOptional

func (sig *Signature) WithOptional(mask ...bool) *Signature

WithOptional marks specific parameters as optional (fluent interface)

func (*Signature) WithOptionalAt

func (sig *Signature) WithOptionalAt(indices ...int) *Signature

WithOptionalAt marks specific parameter indices as optional (fluent interface)

func (*Signature) WithRest

func (sig *Signature) WithRest(restType Type) *Signature

WithRest adds a rest parameter type (automatically wrapped in ArrayType if needed)

type TemplateLiteralPart

type TemplateLiteralPart struct {
	IsLiteral bool   // true for string literals, false for type interpolations
	Literal   string // string content (when IsLiteral=true)
	Type      Type   // interpolated type (when IsLiteral=false)
}

TemplateLiteralPart represents a part of a template literal type

type TemplateLiteralType

type TemplateLiteralType struct {
	Parts []TemplateLiteralPart // Alternating string and type parts
}

TemplateLiteralType represents a template literal type like `Hello ${T}!` This is used for string manipulation at the type level

func (*TemplateLiteralType) Equals

func (tlt *TemplateLiteralType) Equals(other Type) bool

func (*TemplateLiteralType) String

func (tlt *TemplateLiteralType) String() string

type TupleType

type TupleType struct {
	ElementTypes     []Type // Types of each tuple element (like ParameterTypes in FunctionType)
	OptionalElements []bool // Which elements are optional [string, number?] (like OptionalParams in FunctionType)
	RestElementType  Type   // Type for rest elements [string, ...number[]] (like RestParameterType in FunctionType)
}

TupleType represents a tuple type with fixed-length, ordered elements. Design mirrors FunctionType's parameter structure for compatibility with spread syntax.

func (*TupleType) Equals

func (tt *TupleType) Equals(other Type) bool

func (*TupleType) String

func (tt *TupleType) String() string

type Type

type Type interface {
	// String returns a string representation of the type, suitable for debugging or printing.
	String() string
	// Equals checks if this type is structurally equivalent to another type.
	Equals(other Type) bool
	// contains filtered or unexported methods
}

Type is the interface implemented by all type representations.

func DeeplyWidenType

func DeeplyWidenType(t Type) Type

deeplyWidenObjectType creates a new ObjectType where literal property types are widened. Returns the original type if it's not an ObjectType.

func GetEffectiveType

func GetEffectiveType(t Type) Type

GetEffectiveType resolves aliases and returns the actual type

func GetPropertyType

func GetPropertyType(objectType Type, propertyName string, isOptionalChaining bool) Type

GetPropertyType returns the type of a property access on the given type isOptionalChaining determines whether to be permissive about missing properties

func GetPropertyTypeFromIntersection

func GetPropertyTypeFromIntersection(intersection *IntersectionType, propertyName string) Type

GetPropertyTypeFromIntersection returns the type of a property accessed on an intersection type. The property exists if it exists on ANY of the constituent types. The resulting type is the intersection of the property types from all types that have it.

func GetReadonlyInnerType

func GetReadonlyInnerType(t Type) Type

GetReadonlyInnerType extracts the inner type from a readonly type Returns the type itself if it's not readonly

func GetTypeofResult

func GetTypeofResult(t Type) Type

GetTypeofResult returns the TypeScript-compatible string literal representing the result of the typeof operator when applied to a value of the given type

func GetWidenedType

func GetWidenedType(t Type) Type

GetWidenedType converts literal types to their corresponding primitive base types. Other types are returned unchanged.

func NewIntersectionType

func NewIntersectionType(ts ...Type) Type

NewIntersectionType creates a new intersection type from the given types. It flattens nested intersections and handles simplifications.

func NewUnionType

func NewUnionType(ts ...Type) Type

NewUnionType creates a new union type from the given types. It flattens nested unions and removes duplicate types using structural equality.

func RemoveNullUndefined

func RemoveNullUndefined(t Type) Type

RemoveNullUndefined removes null and undefined from a type. If the type is a union, it filters out null and undefined members. If the type is exactly null or undefined, returns never. Otherwise returns the original type unchanged.

func SimplifyIntersectionWithObjects

func SimplifyIntersectionWithObjects(intersection *IntersectionType) Type

SimplifyIntersectionWithObjects attempts to merge object types in an intersection. This is one of the most complex parts of intersection type handling.

func WidenType

func WidenType(t Type) Type

WidenType converts literal types to their primitive equivalents

type TypeAliasForwardReference

type TypeAliasForwardReference struct {
	AliasName string
}

TypeAliasForwardReference represents a forward reference to a type alias being defined

func (*TypeAliasForwardReference) Equals

func (tafr *TypeAliasForwardReference) Equals(other Type) bool

func (*TypeAliasForwardReference) String

func (tafr *TypeAliasForwardReference) String() string

type TypeParameter

type TypeParameter struct {
	Name       string // The parameter name (e.g., "T", "U", "K", "V")
	Constraint Type   // Optional constraint (e.g., T extends string), nil if unconstrained
	Default    Type   // Optional default type (e.g., T = string), nil if no default
	Index      int    // Position in the type parameter list (0-based)
}

TypeParameter represents a generic type parameter (e.g., T in Array<T> or function<T>)

func NewTypeParameter

func NewTypeParameter(name string, index int, constraint Type) *TypeParameter

NewTypeParameter creates a new type parameter

func (*TypeParameter) String

func (tp *TypeParameter) String() string

type TypeParameterType

type TypeParameterType struct {
	Parameter *TypeParameter // Reference to the parameter definition
}

TypeParameterType represents a reference to a type parameter within a generic type or function This is what gets used inside the generic body (e.g., the "T" in "return x: T")

func (*TypeParameterType) Equals

func (t *TypeParameterType) Equals(other Type) bool

func (*TypeParameterType) String

func (t *TypeParameterType) String() string

type TypePredicateType

type TypePredicateType struct {
	ParameterName string // The parameter being tested (e.g., "x" in "x is string")
	Type          Type   // The type being tested for
}

TypePredicateType represents a type predicate like 'x is string' This is used in function return types to indicate type guards

func (*TypePredicateType) Equals

func (tpt *TypePredicateType) Equals(other Type) bool

func (*TypePredicateType) String

func (tpt *TypePredicateType) String() string

type TypeofType

type TypeofType struct {
	Identifier string // The identifier whose type we're extracting
}

TypeofType represents a typeof type operator like typeof someVariable This extracts the type of a value from the type environment

func (*TypeofType) Equals

func (tt *TypeofType) Equals(other Type) bool

func (*TypeofType) String

func (tt *TypeofType) String() string

type UnionType

type UnionType struct {
	Types []Type // Slice holding the types in the union

}

UnionType represents a union of multiple types (e.g., string | number). Stores constituent types in a slice.

func (*UnionType) ContainsType

func (ut *UnionType) ContainsType(target Type) bool

ContainsType checks if the union contains a type that equals the given type

func (*UnionType) Equals

func (ut *UnionType) Equals(other Type) bool

func (*UnionType) RemoveType

func (ut *UnionType) RemoveType(target Type) Type

RemoveType returns a new union with the specified type removed Returns the modified union type, or the single remaining type if only one remains

func (*UnionType) String

func (ut *UnionType) String() string

Jump to

Keyboard shortcuts

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