import * as Flags from './common/flags'; import { InterfaceToken, RtVoidType, RtUnknownType, RtAnyType, RtParameter } from './common'; /** * This has no effect because the Interface is already reified. * Simply use the Interface as is or use reflect() on the reified interface. */ export declare function reify(value: InterfaceToken): InterfaceToken; /** * Obtain an object which uniquely identifies an interface type. * You may prefer reflect() if you are writing reflect(reify()) or * ReflectedClass.from(reify()) */ export declare function reify(): InterfaceToken; declare type Literal = true | false | null | number | string; declare type RtTypeRef = RtType | Function | Literal | InterfaceToken; interface RtType { TΦ: string; } interface RtUnionType { TΦ: typeof Flags.T_UNION; t: RtTypeRef[]; } interface RtIntersectionType { TΦ: typeof Flags.T_INTERSECTION; t: RtTypeRef[]; } interface RtArrayType { TΦ: typeof Flags.T_ARRAY; e: RtTypeRef; } interface RtTupleElement { n: string; t: RtTypeRef; } interface RtTupleType { TΦ: typeof Flags.T_TUPLE; e: RtTupleElement[]; } interface RtGenericRef { TΦ: typeof Flags.T_GENERIC; t: RtTypeRef; p: RtTypeRef[]; } export declare type ReflectedTypeRefKind = 'union' | 'intersection' | 'any' | 'unknown' | 'tuple' | 'array' | 'class' | 'any' | 'unknown' | 'generic' | 'literal' | 'void' | 'interface'; export declare const TYPE_REF_KIND_EXPANSION: Record; export declare class ReflectedTypeRef { private _ref; toString(): string; /** * Check if the given value matches this type reference. Collects any errors into the `errors` list. * @param value * @param errors * @param context * @returns */ matchesValue(value: any, errors?: Error[], context?: string): boolean; private static kinds; get kind(): ReflectedTypeRefKind; /** * Checks if this type reference is a Promise, optionally a Promise of a specific type. * If the type reference does not specify a type of Promise but you have provided a type * to check for, this will return false. * @param klass The type of promise to check for. Would be String when looking for Promise */ isPromise(klass?: Constructor): this is ReflectedGenericRef; /** * Checks if this type reference is a class. Note: If the class reference has type parameters, * (ie it is generic) this check will fail, and instead isGeneric() will succeed. * @param klass */ isClass(klass?: Constructor): this is ReflectedClassRef; isInterface(interfaceType: InterfaceToken): this is ReflectedInterfaceRef; /** * Checks if this type is a literal type (null/true/false/undefined or a literal expression) * @param value * @returns */ isLiteral(value: T): this is ReflectedLiteralRef; isLiteral(): this is ReflectedLiteralRef; /** Check if this type reference is an interface type */ is(kind: 'interface'): this is ReflectedInterfaceRef; /** Check if this type reference is a class type */ is(kind: 'class'): this is ReflectedClassRef; /** Check if this type reference is a generic type */ is(kind: 'generic'): this is ReflectedGenericRef; /** Check if this type reference is an array type */ is(kind: 'array'): this is ReflectedArrayRef; /** Check if this type reference is an intersection type */ is(kind: 'intersection'): this is ReflectedIntersectionRef; /** Check if this type reference is a union type */ is(kind: 'union'): this is ReflectedUnionRef; /** Check if this type reference is a tuple type */ is(kind: 'tuple'): this is ReflectedTupleRef; /** Check if this type reference is a void type */ is(kind: 'void'): this is ReflectedVoidRef; /** Check if this type reference is an any type */ is(kind: 'any'): this is ReflectedAnyRef; /** Check if this type reference is an unknown type */ is(kind: 'unknown'): this is ReflectedUnknownRef; /** Check if this type reference is a literal type */ is(kind: 'literal'): this is ReflectedLiteralRef; /** * Check if this type reference is an instance of the given ReflectedTypeRef subclass. * @param type The subclass of ReflectedTypeRef to check */ is(this: T, type: Constructor): this is U; /** * Assert that this type reference is an interface type and cast it to ReflectedInterfaceRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'interface'): ReflectedInterfaceRef; /** * Assert that this type reference is a class type and cast it to ReflectedClassRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'class'): ReflectedClassRef; /** * Assert that this type reference is a generic type and cast it to ReflectedGenericRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'generic'): ReflectedGenericRef; /** * Assert that this type reference is an array type and cast it to ReflectedArrayRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'array'): ReflectedArrayRef; /** * Assert that this type reference is an intersection type and cast it to ReflectedIntersectionRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'intersection'): ReflectedIntersectionRef; /** * Assert that this type reference is a union type and cast it to ReflectedUnionRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'union'): ReflectedUnionRef; /** * Assert that this type reference is a tuple type and cast it to ReflectedTupleRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'tuple'): ReflectedTupleRef; /** * Assert that this type reference is a void type and cast it to ReflectedVoidRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'void'): ReflectedVoidRef; /** * Assert that this type reference is a void type and cast it to ReflectedVoidRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'unknown'): ReflectedUnknownRef; /** * Assert that this type reference is a void type and cast it to ReflectedVoidRef. * If the reference is not the correct type an error is thrown. */ as(kind: 'any'): ReflectedAnyRef; /** * Assert that this type reference is the given ReflectedTypeRef subclass. * If the reference is not the correct type an error is thrown. */ as(this: T, subclass: Constructor): U; isVoid(): this is ReflectedVoidRef; isNull(): this is ReflectedLiteralRef; isUndefined(): this is ReflectedLiteralRef; isTrue(): this is ReflectedLiteralRef; isFalse(): this is ReflectedLiteralRef; isStringLiteral(): this is ReflectedLiteralRef; isNumberLiteral(): this is ReflectedLiteralRef; isBooleanLiteral(): this is ReflectedLiteralRef; /** * Check if this type reference is a generic type, optionally checking if the generic's * base type is the given class. For instance isGeneric(Promise) is true for Promise. * @param klass */ isGeneric(klass?: Constructor): this is ReflectedGenericRef; isUnion(elementDiscriminator?: (elementType: ReflectedTypeRef) => boolean): this is ReflectedUnionRef; isIntersection(elementDiscriminator?: (elementType: ReflectedTypeRef) => boolean): this is ReflectedIntersectionRef; isArray(elementDiscriminator?: (elementType: ReflectedTypeRef) => boolean): this is ReflectedArrayRef; isTuple(elementDiscriminators?: ((elementType: ReflectedTupleElement) => boolean)[]): this is ReflectedTupleRef; isUnknown(): boolean; isAny(): boolean; } export declare class ReflectedClassRef extends ReflectedTypeRef> { get kind(): "class"; get class(): Constructor; toString(): string; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedInterfaceRef extends ReflectedTypeRef { get kind(): "interface"; get token(): InterfaceToken; toString(): string; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedLiteralRef extends ReflectedTypeRef> { get kind(): "literal"; get value(): any; toString(): string; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedUnionRef extends ReflectedTypeRef { get kind(): "union"; toString(): string; private _types; get types(): ReflectedTypeRef[]; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedIntersectionRef extends ReflectedTypeRef { get kind(): "intersection"; toString(): string; private _types; get types(): ReflectedTypeRef[]; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedArrayRef extends ReflectedTypeRef { get kind(): "array"; toString(): string; private _elementType; get elementType(): ReflectedTypeRef; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedVoidRef extends ReflectedTypeRef { get kind(): "void"; toString(): string; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedUnknownRef extends ReflectedTypeRef { get kind(): "unknown"; toString(): string; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedAnyRef extends ReflectedTypeRef { get kind(): "unknown"; toString(): string; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedTupleRef extends ReflectedTypeRef { get kind(): "tuple"; toString(): string; private _types; get elements(): ReflectedTupleElement[]; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedGenericRef extends ReflectedTypeRef { get kind(): "generic"; toString(): string; private _baseType; get baseType(): ReflectedTypeRef; private _typeParameters; get typeParameters(): ReflectedTypeRef[]; matchesValue(value: any, errors?: Error[], context?: string): boolean; } export declare class ReflectedTupleElement { readonly ref: Readonly; constructor(ref: Readonly); toString(): string; get name(): string; private _type; get type(): ReflectedTypeRef; } export declare class ReflectedFlags { constructor(flags: string); private flagToProperty; private propertyToFlag; isReadonly: boolean; isAbstract: boolean; isPublic: boolean; isPrivate: boolean; isProtected: boolean; isProperty: boolean; isMethod: boolean; isClass: boolean; isInterface: boolean; isOptional: boolean; isAsync: boolean; isExported: boolean; toString(): string; } export declare type Visibility = 'public' | 'private' | 'protected'; /** * Reflection data for a parameter */ export declare class ReflectedParameter { readonly rawMetadata: RtParameter; constructor(rawMetadata: RtParameter); private _flags; /** * Get the unmangled original name for this parameter */ get name(): string; private _type; /** * Get the reflected type of this parameter */ get type(): ReflectedTypeRef; /** * Get flags that define aspects of this property. */ get flags(): ReflectedFlags; /** * True if this parameter is optional */ get isOptional(): boolean; /** * Retrieve the initializer for this parameter. Invoking the initializer produces the * default value for the parameter. Caution: The initializer depends on the value of 'this'. * Use evaluateInitializer() to properly invoke the initializer. */ get initializer(): () => ValueT; /** * Evaluate the initializer for this parameter with the given value for 'this'. If not provided, * 'this' is an empty object. This is suitable for constructor parameters but instance method parameters * may reference properties of the object, and so getting the correct value may require passing an * appropriate instance. * * @param thisObject * @returns */ evaluateInitializer(thisObject?: any): any; } /** * Reflection data for a method parameter */ export declare class ReflectedMethodParameter extends ReflectedParameter { readonly method: ReflectedMethod; readonly rawMetadata: RtParameter; constructor(method: ReflectedMethod, rawMetadata: RtParameter); } /** * Reflection data for a method parameter */ export declare class ReflectedFunctionParameter extends ReflectedParameter { readonly func: ReflectedFunction; readonly rawMetadata: RtParameter; constructor(func: ReflectedFunction, rawMetadata: RtParameter); } /** * Reflection data for a constructor parameter */ export declare class ReflectedConstructorParameter extends ReflectedParameter { readonly reflectedClass: ReflectedClass; readonly rawMetadata: RtParameter; constructor(reflectedClass: ReflectedClass, rawMetadata: RtParameter); private _class; /** * Retrieve the reflected class that this constructor parameter is defined on. */ get class(): ReflectedClass; /** * True if this constructor parameter is declared readonly, meaning it is * also an instance property of the class. */ get isReadonly(): boolean; /** * True if this constructor parameter is declared public, meaning it is * also an instance property of the class. */ get isPublic(): boolean; /** * True if this constructor parameter is declared protected, meaning it is * also an instance property of the class. */ get isProtected(): boolean; /** * True if this constructor parameter is declared private, meaning it is * also an instance property of the class. */ get isPrivate(): boolean; /** * Get visibility of this constructor parameter. If the constructor * parameter has no visibility modifiers, this is null. */ get visibility(): Visibility; /** * True if the constructor parameter is also a property. */ get isProperty(): boolean; } /** * Reflection data for a class member */ export declare class ReflectedMember { readonly name: string; readonly isStatic: boolean; constructor(reflectedClass: ReflectedClass, name: string, isStatic: boolean); private _class; private _flags; /** * Get the given metadata key for this member. This is equivalent to * Reflect.getMetadata(key, this.host, this.name) * @param key * @returns */ getMetadata(key: string): T; /** * Define a metadata key for this member. This is equivalent to * Reflect.defineMetadata(key, value, this.host, this.name) * @param key * @returns */ defineMetadata(key: string, value: T): void; /** * Check if a metadata key exists for this member. This is equivalent to * Reflect.hasMetadata(key, this.host, this.name) * @param key * @returns */ hasMetadata(key: string): boolean; /** * Get the host object for this method. For static members this is the * class constructor. For instance members this is the class's prototype. */ get host(): any; /** * Get the reflected class that hosts this member */ get class(): ReflectedClass; /** * Get the flags for this member. Includes modifiers and other properties about * the member. */ get flags(): Readonly; /** * True if this member is abstract. */ get isAbstract(): boolean; /** * True if this member has private visibility. */ get isPrivate(): boolean; /** * True if this member has public visibility. */ get isPublic(): boolean; /** * True if this member is specifically marked as public * (as opposed to default visibility). */ get isMarkedPublic(): boolean; /** * True if this member has protected visibility. */ get isProtected(): boolean; /** * Get the visibility (accessibility) of this member. * Can be 'public', 'protected', or 'private' */ get visibility(): Visibility; /** * Whether this member is marked as optional. */ get isOptional(): boolean; } export declare class ReflectedFunction { readonly func: T; private constructor(); private _flags; private _returnType; private _RtParameter; private _parameters; private static reflectedFunctions; static for(func: Function): ReflectedFunction; matchesValue(object: any, errors?: Error[], context?: string): boolean; /** * Check if the function has the given metadata key defined. This is equivalent * to Reflect.hasMetadata(key, value, this.func) * @param key * @returns */ hasMetadata(key: string): boolean; /** * Get the specified metadata key for this function. This is equivalent * to Reflect.getMetadata(key, value, this.func) * @param key * @returns */ getMetadata(key: string): T; /** * Define a metadata key for this function. This is equivalent * to Reflect.defineMetadata(key, value, this.func) * @param key The metadata key to define. * @param value */ defineMetadata(key: string, value: T): void; /** * Get the flags for this function. */ get flags(): Readonly; /** * Names of the parameters for this function. */ get parameterNames(): string[]; private _parameterTypes; /** * Types for the parameter types of this function. */ get parameterTypes(): ReflectedTypeRef[]; /** * Retrieve the set of reflected parameters for this method. */ get parameters(): ReflectedFunctionParameter[]; /** * Get the parameter with the specified name * @param name * @returns The reflected parameter */ getParameter(name: string): any; /** * Retrieve the return type of this function. */ get returnType(): ReflectedTypeRef; /** * True if this function is declared as async. */ get isAsync(): boolean; } /** * Reflection data for a class method */ export declare class ReflectedMethod extends ReflectedMember { private _returnType; private _RtParameter; private _parameters; matchesValue(object: any, errors?: Error[], context?: string): boolean; get func(): any; /** * Retrieve the reflected method for the given method function. * If the function is not a method, a TypeError is thrown. * @param method */ static for(method: Function): ReflectedMethod; /** * Retrieve an array with the parameter names for this method. */ get parameterNames(): string[]; private _parameterTypes; /** * Retrieve an array with the parameter types for this method. */ get parameterTypes(): ReflectedTypeRef[]; /** * Retrieve the set of reflected parameters for this method. */ get parameters(): ReflectedMethodParameter[]; /** * Get a reflected parameter by name * @param name * @returns The reflected parameter */ getParameter(name: string): any; /** * Get the return type of this method. */ get returnType(): ReflectedTypeRef; /** * True if this method is declared as async. */ get isAsync(): boolean; } /** * Represents a constructor for a specific type. */ export interface Constructor extends Function { new (...args: any[]): T; } /** * Represents a reflected property of a class or interface. */ export declare class ReflectedProperty extends ReflectedMember { private _type; /** * Get the type of this property. */ get type(): ReflectedTypeRef; /** * True if this property is marked readonly. */ get isReadonly(): boolean; /** * Check if the given value matches the type of this property, and would * thus be a valid assignment. * @param object * @param errors * @returns */ matchesValue(object: any, errors?: Error[]): boolean; } /** * Provides access to the known runtime type metadata for a particular class * or Interface value (as obtained by reify()). */ export declare class ReflectedClass { /** * Constructs a new ReflectedClass. Use ReflectedClass.for() to obtain a ReflectedClass. */ private constructor(); /** * Obtain a ReflectedClass for the given class constructor, Interface value or instance. * @param constructorOrValue Can be a class constructor, Interface, or an instance of a class (in which case the * instance's constructor will be used) * @returns The ReflectedClass. */ static for(constructorOrValue: Constructor | InterfaceToken | Function | InstanceType>): ReflectedClass; private static reflectedClasses; private static forConstructorOrInterface; private _class; private _ownMethods; private _methods; private _ownPropertyNames; private _ownMethodNames; private _methodNames; private _super; private _RtParameter; private _parameters; private _ownProperties; private _properties; private _flags; private _interfaces; /** * Get the interfaces that this class implements. */ get interfaces(): ReflectedTypeRef[]; /** * Check if this class implements the given interface. The parameter can be a reified interface * reference or a class reference. Note that implementing a class is not the same as extending a class. * * @param interfaceType * @returns boolean */ implements(interfaceType: InterfaceToken | Constructor): boolean; /** * Check if the given value matches the shape of this type, and thus would be a valid assignment. * @param object * @param errors * @returns */ matchesValue(object: any, errors?: Error[]): boolean; /** * Get the prototype object for this reflected class. */ get prototype(): any; /** * Get the class constructor for this reflected class. */ get class(): InterfaceToken | Constructor; /** * Get the reflected superclass for this class. */ get super(): ReflectedClass; private _hasPropertyNamesMeta; /** * Check if the function has the given metadata key defined. This is equivalent * to Reflect.hasMetadata(key, this.class) * @param key * @returns */ hasMetadata(key: string): boolean; /** * Get the specified metadata key. This is equivalent to Reflect.getMetadata(key, this.class). * @param key * @returns */ getMetadata(key: string): T; /** * Define a metadata key on this class. This is equivalent to Reflect.defineMetadata(key, value, this.class). * @param key * @param value * @returns */ defineMetadata(key: string, value: T): T; /** * Define a metadata key on this class's prototype object. This is equivalent to Reflect.defineMetadata(key, value, this.class.prototype) * @param key * @param value * @returns */ definePrototypeMetadata(key: string, value: T): T; /** * Retrieve the set of property names that are defined directly on this class, excluding * those which are inherited. */ get ownPropertyNames(): string[]; /** * Retrieve the set of method names that are defined directly on this class, excluding * those which are inherited. */ get ownMethodNames(): string[]; private _ownStaticPropertyNames; private _hasStaticPropertyNameMeta; /** * Retrieve the set of static property names that are defined directly on this class, excluding * those which are inherited. Always empty for interfaces. */ get ownStaticPropertyNames(): string[]; private _ownStaticMethodNames; /** * Retrieve the set of static method names that are defined directly on this class, * excluding those which are inherited. Always empty for interfaces */ get ownStaticMethodNames(): string[]; /** * Retrieve the set of flags for this class/interface. Use this to check for modifiers or other properties * of the class/interface. */ get flags(): Readonly; /** * True if the class is marked abstract. */ get isAbstract(): boolean; /** * Get the instance method names for this reflected class/interface. */ get methodNames(): string[]; private _staticPropertyNames; /** * Get the static property names defined for this reflected class. Always empty for interfaces. */ get staticPropertyNames(): any; private _staticMethodNames; /** * Retrieve an array of the names of static methods defined on this reflected class. * Always empty for interfaces. */ get staticMethodNames(): string[]; private _propertyNames; /** * Retrieve an array of the names of instance properties defined on this class/interface */ get propertyNames(): string[]; /** * Retrieve the set of reflected methods defined directly on this class/interface. */ get ownMethods(): ReflectedMethod[]; private _ownStaticProperties; /** * Retrieve the set of reflected static properties defined directly on this class. Always empty * for interfaces. */ get ownStaticProperties(): ReflectedProperty[]; private _ownStaticMethods; /** * Retrieve the set of reflected static methods defined directly on this class. Always * empty for interfaces. */ get ownStaticMethods(): ReflectedMethod[]; /** * Retrieve the set of reflected instance methods defined on this class/interface. */ get methods(): ReflectedMethod[]; private _staticProperties; /** * Retrieve the set of reflected static properties defined on this class. Always * empty for interfaces. */ get staticProperties(): any; private _staticMethods; /** * Retrieve the set of reflected static methods defined on this class. Always * empty for interfaces */ get staticMethods(): ReflectedMethod[]; /** * Retrieve the set of reflected instance properties defined directly on this class/interface */ get ownProperties(): ReflectedProperty[]; /** * Retrieve the set of reflected instance methods defined on this class/interface */ get properties(): ReflectedProperty[]; private get RtParameter(); /** * Retrieve an array of the parameter names for this class's constructor. */ get parameterNames(): string[]; /** * Retrieve an array of the types for the parameters of this class's * constructor. */ get parameterTypes(): (() => any)[]; /** * Retrieve the set of reflected parameters for this class's constructor. */ get parameters(): ReflectedConstructorParameter[]; /** * Get a reflected constructor parameter by name. * @param name * @returns */ getParameter(name: string): ReflectedConstructorParameter; /** * Get a reflected instance method (declared directly on this class) by name * @param name * @returns */ getOwnMethod(name: string): ReflectedMethod; /** * Get a reflected instance method by name * @param name * @returns */ getMethod(name: string): ReflectedMethod; /** * Get a reflected static method by name * @param name * @returns */ getStaticMethod(name: string): ReflectedMethod; private _dynamicStaticProperties; /** * Get a reflected static property (declared directly on this class) by name * @param name * @returns */ getOwnStaticProperty(name: string): ReflectedProperty; /** * Get a reflected static property by name * @param name * @returns */ getStaticProperty(name: string): any; /** * Get a reflected instance property (declared directly on this class) by name * @param name * @returns */ getOwnProperty(name: string): ReflectedProperty; private _dynamicProperties; /** * Get a reflected instance property by name * @param name * @returns */ getProperty(name: string): ReflectedProperty; } /** * Returns true if the class (or the class of the given value) implement the given interface. * Note that interfaceType can be a class constructor. Implementing a class is not the same as extending a class. * * @param value The value to check. Can be a constructor or a value (whose constructor will be checked) * @param interfaceType The interface type to use. Can be a class constructor or an Interface object. * @returns True if the interface is implemented */ export declare function implementsInterface(value: any, interfaceType: InterfaceToken | Constructor): boolean; /** * Returns true if the given value matches the shape of the interface / class passed as interfaceType. * * @param value * @param interfaceType * @returns True if the value is the correct shape */ export declare function matchesShape(value: any, interfaceType: InterfaceToken | Constructor): boolean; /** * Get the reflected interface object for the given interface (identified by T) * @returns The reflected interface */ export declare function reflect(): ReflectedClass | Constructor>; /** * Get the reflected class for the given constructor or instance. * @param value A constructor, Interface value, or an instance of a class * @returns The reflected class */ export declare function reflect(value: Constructor): ReflectedClass>; export declare function reflect(value: T): (ReflectedFunction | ReflectedMethod); export declare function reflect(value: T): ReflectedClass>; export {}; //# sourceMappingURL=reflect.d.ts.map