import { ClassType } from '../core/index.js'; import { TypeNumberBrand } from '../type-spec/index.js'; import { Annotations } from '../type-annotations.js'; import { ReceiveType, ReflectionClass } from './reflection.js'; export * from '../type-annotations.js'; export declare enum ReflectionVisibility { public = 0, protected = 1, private = 2 } export declare enum ReflectionKind { never = 0, any = 1, unknown = 2, void = 3, object = 4, string = 5, number = 6, boolean = 7, symbol = 8, bigint = 9, null = 10, undefined = 11, regexp = 12, literal = 13, templateLiteral = 14, property = 15, method = 16, function = 17, parameter = 18, promise = 19, /** * Uint8Array, Date, custom classes, Set, Map, etc */ class = 20, typeParameter = 21, enum = 22, union = 23, intersection = 24, array = 25, tuple = 26, tupleMember = 27, enumMember = 28, rest = 29, objectLiteral = 30, indexSignature = 31, propertySignature = 32, methodSignature = 33, infer = 34, callSignature = 35 } export type TypeDecorator = (annotations: Annotations, decorator: TypeObjectLiteral) => boolean; /** * @reflection never */ export interface TypeAnnotations { id?: number; origin?: Type; /** * True when this type comes from an inline type, e.g. * * `type A = T;`. Type of `T` is inlined. * `type A = {}`. Type of `{}` is not inlined. * * If the type is not inlined and the result of a type function, then we assign parents of members accordingly. This is not the caee when a type was inlined. */ inlined?: true; /** * If the type was created by a type function, this contains the alias name. */ typeName?: string; /** * If the type was created by a type function, this contains the arguments passed the function. */ typeArguments?: Type[]; /** * Set for index access expressions, e.g. Config['property']. */ indexAccessOrigin?: { container: TypeClass | TypeObjectLiteral; index: Type; }; /** * type User = {id: number, user: string}; * type UserCreate = Pick; * typeOf().originTypes[0].typeName = 'Pick' * typeOf().originTypes[0].typeArguments = [User, 'user'] */ originTypes?: { typeName: string; typeArguments?: Type[]; }[]; annotations?: Annotations; decorators?: Type[]; scheduleDecorators?: TypeObjectLiteral[]; /** * A place where arbitrary jit functions and its cache data is stored. */ jit?: JitContainer; } export declare function applyScheduledAnnotations(type: Type): void; export declare function hasTypeInformation(object: ClassType | Function): boolean; /** * Object to hold runtime jit data. */ export type JitContainer = any; export declare function getTypeJitContainer(type: Type): JitContainer; export declare function clearTypeJitContainer(type: Type): void; export interface TypeNever extends TypeAnnotations { kind: ReflectionKind.never; parent?: Type; } export interface TypeAny extends TypeAnnotations { kind: ReflectionKind.any; parent?: Type; } export interface TypeUnknown extends TypeAnnotations { kind: ReflectionKind.unknown; parent?: Type; } export interface TypeVoid extends TypeAnnotations { kind: ReflectionKind.void; parent?: Type; } export interface TypeObject extends TypeAnnotations { kind: ReflectionKind.object; parent?: Type; } export interface TypeString extends TypeAnnotations { kind: ReflectionKind.string; parent?: Type; } export declare function isIntegerType(type: Type): type is TypeNumber; export interface TypeNumber extends TypeAnnotations { kind: ReflectionKind.number; brand?: TypeNumberBrand; parent?: Type; } export interface TypeBoolean extends TypeAnnotations { kind: ReflectionKind.boolean; parent?: Type; } export interface TypeBigInt extends TypeAnnotations { kind: ReflectionKind.bigint; parent?: Type; } export interface TypeSymbol extends TypeAnnotations { kind: ReflectionKind.symbol; parent?: Type; } export interface TypeNull extends TypeAnnotations { kind: ReflectionKind.null; parent?: Type; } export interface TypeUndefined extends TypeAnnotations { kind: ReflectionKind.undefined; parent?: Type; } export interface TypeLiteral extends TypeAnnotations { kind: ReflectionKind.literal; literal: symbol | string | number | boolean | bigint | RegExp; parent?: Type; } export interface TypeTemplateLiteral extends TypeAnnotations { kind: ReflectionKind.templateLiteral; types: (TypeString | TypeAny | TypeNumber | TypeLiteral | TypeInfer)[]; parent?: Type; } export interface TypeRegexp extends TypeAnnotations { kind: ReflectionKind.regexp; parent?: Type; } export interface TypeBaseMember extends TypeAnnotations { visibility: ReflectionVisibility; abstract?: true; static?: true; optional?: true; readonly?: true; } export interface TypeParameter extends TypeAnnotations { kind: ReflectionKind.parameter; name: string; type: Type; parent: TypeFunction | TypeMethod | TypeMethodSignature | TypeCallSignature; visibility?: ReflectionVisibility; readonly?: true; optional?: true; description?: string; /** * Set when the parameter has a default value aka initializer. */ default?: () => any; } export interface TypeMethod extends TypeBaseMember { kind: ReflectionKind.method; parent: TypeClass; name: number | string | symbol; description?: string; parameters: TypeParameter[]; return: Type; } export interface TypeProperty extends TypeBaseMember { kind: ReflectionKind.property; parent: TypeClass; visibility: ReflectionVisibility; name: number | string | symbol; description?: string; type: Type; /** * Set when the property has a default value aka initializer. */ default?: () => any; } export interface TypeFunction extends TypeAnnotations { kind: ReflectionKind.function; parent?: Type; name?: number | string | symbol; description?: string; function?: Function; parameters: TypeParameter[]; return: Type; } export interface TypeCallSignature extends TypeAnnotations { kind: ReflectionKind.callSignature; parent?: Type; parameters: TypeParameter[]; return: Type; } export interface TypePromise extends TypeAnnotations { kind: ReflectionKind.promise; parent?: Type; type: Type; } export interface TypeClass extends TypeAnnotations { kind: ReflectionKind.class; parent?: Type; classType: ClassType; description?: string; /** * When the class extends another class and uses on it generic type arguments, then those arguments * are in this array. * For example `class A extends B {}` then extendsArguments = [string, boolean]. * The reference to `B` is not part of TypeClass since this information is available in JavaScript runtime * by using `Object.getPrototypeOf(type.classType)`. */ extendsArguments?: Type[]; /** * When the class implements another interface/type, then those types are in this array. * * For example `class A implements B {}` then implements = [B]. */ implements?: Type[]; /** * When class has generic type arguments, e.g. MyClass, it contains * all type arguments. If no type arguments are given, it's undefined. */ arguments?: Type[]; /** * properties/methods. */ types: (TypeIndexSignature | TypeProperty | TypeMethod)[]; } export interface TypeEnum extends TypeAnnotations { kind: ReflectionKind.enum; parent?: Type; enum: { [name: string]: string | number | undefined | null; }; values: (string | number | undefined | null)[]; indexType: Type; description?: string; } export interface TypeEnumMember extends TypeAnnotations { kind: ReflectionKind.enumMember; parent: TypeEnum; name: string; default?: () => string | number; } export interface TypeTypeParameter extends TypeAnnotations { kind: ReflectionKind.typeParameter; parent?: Type; name: string; } export interface TypeUnion extends TypeAnnotations { kind: ReflectionKind.union; parent?: Type; types: Type[]; } export interface TypeIntersection extends TypeAnnotations { kind: ReflectionKind.intersection; parent?: Type; types: Type[]; } export interface TypeArray extends TypeAnnotations { kind: ReflectionKind.array; parent?: Type; type: Type; } export interface TypePropertySignature extends TypeAnnotations { kind: ReflectionKind.propertySignature; parent: TypeObjectLiteral; name: number | string | symbol; optional?: true; readonly?: true; description?: string; type: Type; } export interface TypeMethodSignature extends TypeAnnotations { kind: ReflectionKind.methodSignature; parent: TypeObjectLiteral; name: number | string | symbol; optional?: true; description?: string; parameters: TypeParameter[]; return: Type; } /** * Object literals or interfaces. */ export interface TypeObjectLiteral extends TypeAnnotations { kind: ReflectionKind.objectLiteral; parent?: Type; description?: string; types: (TypeIndexSignature | TypePropertySignature | TypeMethodSignature | TypeCallSignature)[]; /** * When the interface extends another interface/type, then those types are in this array. * * For example `interface A extends B {}` then implements = [B]. */ implements?: Type[]; } export interface TypeIndexSignature extends TypeAnnotations { kind: ReflectionKind.indexSignature; parent: TypeClass | TypeObjectLiteral; index: Type; type: Type; } export interface TypeInfer extends TypeAnnotations { kind: ReflectionKind.infer; parent?: Type; set(type: Type): void; } export interface TypeTupleMember extends TypeAnnotations { kind: ReflectionKind.tupleMember; parent: TypeTuple; type: Type; optional?: true; name?: string; } export interface TypeTuple extends TypeAnnotations { kind: ReflectionKind.tuple; parent?: Type; types: TypeTupleMember[]; } export interface TypeRest extends TypeAnnotations { kind: ReflectionKind.rest; parent: TypeTypeParameter | TypeTupleMember; type: Type; } /** * @reflection never */ export type Type = TypeNever | TypeAny | TypeUnknown | TypeVoid | TypeObject | TypeString | TypeNumber | TypeBoolean | TypeBigInt | TypeSymbol | TypeNull | TypeUndefined | TypeLiteral | TypeTemplateLiteral | TypeParameter | TypeFunction | TypeMethod | TypeProperty | TypePromise | TypeClass | TypeEnum | TypeEnumMember | TypeUnion | TypeIntersection | TypeArray | TypeObjectLiteral | TypeIndexSignature | TypePropertySignature | TypeMethodSignature | TypeTypeParameter | TypeInfer | TypeTuple | TypeTupleMember | TypeRest | TypeRegexp | TypeCallSignature; export type Widen = T extends string ? string : T extends number ? number : T extends bigint ? bigint : T extends boolean ? boolean : T extends symbol ? symbol : T; export type FindType = T extends { kind: infer K; } ? K extends LOOKUP ? T : never : never; /** * Merge dynamic runtime types with static types. In the type-system resolves as any, in runtime as the correct type. * * ```typescript * const stringType = {kind: ReflectionKind.string}; * type t = {a: InlineRuntimeType} * * const value = 34; * type t = {a: InlineRuntimeType} * ``` */ export type InlineRuntimeType | undefined | ReflectionClass | Type | number | string | boolean | bigint, R = any> = T extends ReflectionClass ? K : T extends ReceiveType ? R : T extends Type ? R : T extends undefined ? never : T; export declare function isType(entry: any): entry is Type; export declare function isBinary(type: Type): boolean; export declare function isPrimitive(type: T): boolean; export declare function isPropertyType(type: Type): type is TypePropertySignature | TypeProperty; /** * Returns true if the type is TypePropertySignature | TypeProperty and not a static member. */ export declare function isPropertyMemberType(type: Type): type is TypePropertySignature | TypeProperty; /** * Return all properties created in the constructor (via `constructor(public title: string)`) * * If a non-property parameter is in the constructor, the type is given instead, e.g. `constructor(public title: string, anotherOne:number)` => [TypeProperty, TypeNumber] */ export declare function getConstructorProperties(type: TypeClass | TypeObjectLiteral): { parameters: (TypeProperty | Type)[]; properties: TypeProperty[]; }; export type WithAnnotations = TypeAny | TypeUnknown | TypeString | TypeNumber | TypeBigInt | TypeBoolean | TypeArray | TypeTuple | TypeLiteral | TypeNull | TypeUndefined | TypeClass | TypeObjectLiteral | TypeObject | TypeTemplateLiteral | TypeRegexp | TypeSymbol; export declare function isWithAnnotations(type: ParentLessType): type is WithAnnotations; export declare function getAnnotations(type: WithAnnotations): Annotations; type StackEntry = { left: Type; right: Type; }; /** * Checks if the structure of a and b are identical. */ export declare function isSameType(a: Type, b: Type, stack?: StackEntry[]): boolean; export declare function addType(container: T, type: Type): T; export declare function isTypeIncluded(types: Type[], type: Type, stack?: StackEntry[]): boolean; /** * `true | (string | number)` => `true | string | number` */ export declare function flatten(type: T): T; /** * Flatten nested union types. */ export declare function flattenUnionTypes(types: Type[]): Type[]; /** * empty union => never * union with one member => member * otherwise the union is returned */ export declare function unboxUnion(union: TypeUnion): Type; export declare function findMember(index: string | number | symbol | TypeTemplateLiteral, types: Type[]): TypePropertySignature | TypeMethodSignature | TypeMethod | TypeProperty | TypeIndexSignature | undefined; interface CStack { iterator: Type[]; i: number; round: number; } export declare function emptyObject(type: Type): boolean; export declare class CartesianProduct { protected stack: CStack[]; private current; private next; toGroup(type: Type): Type[]; add(item: Type): void; calculate(): Type[][]; } /** * Query a container type and return the result. * * container[index] * * e.g. {a: string}['a'] => string * e.g. {a: string, b: number}[keyof T] => string | number * e.g. [string, number][0] => string * e.g. [string, number][number] => string | number */ export declare function indexAccess(container: Type, index: Type): Type; export declare function merge(types: (TypeObjectLiteral | TypeClass)[]): TypeObjectLiteral; export declare function narrowOriginalLiteral(type: Type): Type; type GetArrayElement = [T] extends [Array] ? K : never; type RemoveParent = { [P in K]: T[P] extends Type[] ? RemoveParentHomomorphic>[] : T[P] extends Type ? RemoveParentHomomorphic : T[P]; }; type RemoveParentHomomorphic = RemoveParent>; type RemoveDeepParent = T extends infer K ? RemoveParentHomomorphic : never; export type ParentLessType = RemoveDeepParent; /** * This function does not do a deep copy, only shallow. A deep copy makes it way to inefficient, so much that router.spec.ts takes up to 20-30seconds * to complete instead of barely 30ms. */ export declare function copyAndSetParent(inc: T, parent?: Type): FindType; export declare function widenLiteral(type: Type): Type; export declare function assertType(t: Type | undefined, kind: K): asserts t is FindType; export declare function getClassType(type: Type): ClassType; export declare function isMember(type: Type): type is TypePropertySignature | TypeProperty | TypeMethodSignature | TypeMethod; export declare function hasMember(type: TypeObjectLiteral | TypeClass, memberName: number | string | symbol, memberType?: Type): boolean; export declare function getMember(type: TypeObjectLiteral | TypeClass, memberName: number | string | symbol): TypeMethodSignature | TypeMethod | TypePropertySignature | TypeProperty | void; export declare function getTypeObjectLiteralFromTypeClass(type: T): T extends TypeClass ? TypeObjectLiteral : T; /** * Checks whether `undefined` is allowed as type. */ export declare function isOptional(type: Type): boolean; /** * Whether a property has an initializer/default value. */ export declare function hasDefaultValue(type: Type): boolean; /** * Checks whether `null` is allowed as type. */ export declare function isNullable(type: Type): boolean; export declare function resolveProperty(type: Type): Type; export declare function isDateType(type: Type): boolean; export declare function isSetType(type: Type): boolean; export declare function isMapType(type: Type): boolean; /** * Get the key type of a Map or object literal with index signatures. */ export declare function getKeyType(type: Type): Type; /** * Get the value type of a Map or object literal with index signatures. */ export declare function getValueType(type: Type): Type; /** * Registers a custom type decorator. Custom decorators run before the * built-in one, in reverse registration order, so that they can claim their * own `TypeAnnotation<>` names (which the built-in decorator would otherwise * store as generic `typeAnnotation` entries). A decorator returns `true` when * it handled the given decorator type, which stops further processing. */ export declare function registerTypeDecorator(decorator: TypeDecorator): void; /** * Type annotations are object literals with a single optional __meta in it * that has as type a tuple with the name of the annotation as first entry. * The tuple is intersected with the `never` type to make sure it does not * interfere with type checking. * * The processor has currently implemented to not resolve `never & x` to `never`, * so we still have the intersection type in runtime to resolve __meta correctly. * * ```typescript * type MyAnnotation1 = TypeAnnotation<'myAnnotation'> * type MyAnnotation1 = TypeAnnotation<'myAnnotation', T> * * //under the hood it is: * type lowLevel1 = { __meta?: never & ['myAnnotation'] } * type lowLevel2 = { __meta?: never & ['myAnnotation', T] } * ``` */ export declare function getAnnotationMeta(type: TypeObjectLiteral): { id: string; options: Type; } | undefined; export declare const typeDecorators: TypeDecorator[]; export declare function typeToObject(type?: Type, state?: { stack: Type[]; }): any; export declare function memberNameToString(name: number | string | symbol): string; export declare const binaryTypes: ClassType[]; /** * Returns true if the given type is Date, ArrayBuffer, Uint8Array, etc. */ export declare function isGlobalTypeClass(type: Type): type is TypeClass; /** * Returns true if the given type is TypeClass and references a custom (non-global) class. */ export declare function isCustomTypeClass(type: Type): type is TypeClass; /** * Returns a type predicate that checks if the given type is a class and is of the given classType. * If withInheritance is true, it also checks if the type is a subclass of the given classType. */ export declare function isTypeClassOf(classType: ClassType, withInheritance?: boolean): (type: Type) => boolean; /** * Returns the members of a class or object literal. */ export declare function resolveTypeMembers(type: TypeClass | TypeObjectLiteral): (TypeProperty | TypePropertySignature | TypeMethodSignature | TypeMethod | TypeIndexSignature | TypeCallSignature)[]; export declare function stringifyResolvedType(type: Type): string; export declare function stringifyShortResolvedType(type: Type, stateIn?: Partial): string; /** * Returns all (including inherited) constructor properties of a class. */ export declare function getDeepConstructorProperties(type: TypeClass): TypeParameter[]; /** * Returns the index to `type.values` if the given value is part of the enum, exactly or case-insensitive. * Returns -1 if not found. */ export declare function getEnumValueIndexMatcher(type: TypeEnum): (value: string | number | undefined | null) => number; interface StringifyTypeOptions { showNames: boolean; showFullDefinition: boolean; showDescription: boolean; defaultIsOptional: boolean; showHeritage: boolean; showDefaults: boolean; defaultValues: any; stringify?: (type: Type) => string | undefined; } export declare function stringifyType(type: Type, stateIn?: Partial): string; export declare type __ΩReflectionVisibility = any[]; export declare type __ΩReflectionKind = any[]; export declare type __ΩTypeDecorator = any[]; export declare type __ΩJitContainer = any[]; export declare type __ΩTypeNever = any[]; export declare type __ΩTypeAny = any[]; export declare type __ΩTypeUnknown = any[]; export declare type __ΩTypeVoid = any[]; export declare type __ΩTypeObject = any[]; export declare type __ΩTypeString = any[]; export declare type __ΩTypeNumber = any[]; export declare type __ΩTypeBoolean = any[]; export declare type __ΩTypeBigInt = any[]; export declare type __ΩTypeSymbol = any[]; export declare type __ΩTypeNull = any[]; export declare type __ΩTypeUndefined = any[]; export declare type __ΩTypeLiteral = any[]; export declare type __ΩTypeTemplateLiteral = any[]; export declare type __ΩTypeRegexp = any[]; export declare type __ΩTypeBaseMember = any[]; export declare type __ΩTypeParameter = any[]; export declare type __ΩTypeMethod = any[]; export declare type __ΩTypeProperty = any[]; export declare type __ΩTypeFunction = any[]; export declare type __ΩTypeCallSignature = any[]; export declare type __ΩTypePromise = any[]; export declare type __ΩTypeClass = any[]; export declare type __ΩTypeEnum = any[]; export declare type __ΩTypeEnumMember = any[]; export declare type __ΩTypeTypeParameter = any[]; export declare type __ΩTypeUnion = any[]; export declare type __ΩTypeIntersection = any[]; export declare type __ΩTypeArray = any[]; export declare type __ΩTypePropertySignature = any[]; export declare type __ΩTypeMethodSignature = any[]; export declare type __ΩTypeObjectLiteral = any[]; export declare type __ΩTypeIndexSignature = any[]; export declare type __ΩTypeInfer = any[]; export declare type __ΩTypeTupleMember = any[]; export declare type __ΩTypeTuple = any[]; export declare type __ΩTypeRest = any[]; export declare type __ΩWiden = any[]; export declare type __ΩFindType = any[]; export declare type __ΩInlineRuntimeType = any[]; export declare type __ΩWithAnnotations = any[]; export declare type __ΩParentLessType = any[];