typir
Version:
General purpose type checking library
65 lines • 4.19 kB
TypeScript
/******************************************************************************
* Copyright 2024 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import { TypeGraphListener } from '../graph/type-graph.js';
import { Type } from '../graph/type-node.js';
import { TypeInferenceCollectorListener, TypeInferenceRule, TypeInferenceRuleOptions } from '../services/inference.js';
import { TypirServices, TypirSpecifics } from '../typir.js';
import { TypeDescriptor } from './type-descriptor.js';
/**
* A listener for TypeReferences, who will be informed about the resolved/found type of the current TypeReference.
*/
export interface TypeReferenceListener<T extends Type, Specifics extends TypirSpecifics = TypirSpecifics> {
/**
* Informs when the type of the reference is resolved/found.
* @param reference the currently resolved TypeReference
* @param resolvedType Usually the resolved type is either 'Identifiable' or 'Completed',
* in rare cases this type might be 'Invalid', e.g. if there are corresponding inference rules or TypeInitializers.
*/
onTypeReferenceResolved(reference: TypeReference<T, Specifics>, resolvedType: T): void;
/**
* Informs when the type of the reference is invalidated/removed.
* @param reference the currently invalidate/unresolved TypeReference
* @param previousType undefined occurs in the special case, that the TypeReference never resolved a type so far,
* but new listeners already want to be informed about the (current) type.
*/
onTypeReferenceInvalidated(reference: TypeReference<T, Specifics>, previousType: T | undefined): void;
}
/**
* A TypeReference accepts a specification for a type and searches for the corresponding type in the type system according to this specification.
* Different TypeReferences might resolve to the same Type.
* This class is used during the use case, when a Typir type uses other types for its properties,
* e.g. class types use other types from the type system for describing the types of its fields ("use existing type").
*
* The internal logic of a TypeReference is independent from the kind of the type to resolve.
* A TypeReference takes care of the lifecycle of the types.
*
* Once the type is resolved, listeners are notified about this and all following changes of its state.
*/
export declare class TypeReference<T extends Type, Specifics extends TypirSpecifics = TypirSpecifics> implements TypeGraphListener, TypeInferenceCollectorListener<Specifics> {
protected readonly descriptor: TypeDescriptor<T, Specifics>;
protected readonly services: TypirServices<Specifics>;
protected resolvedType: T | undefined;
/** These listeners will be informed, whenever the resolved state of this TypeReference switched from undefined to an actual type or from an actual type to undefined. */
protected readonly listeners: Array<TypeReferenceListener<T, Specifics>>;
constructor(descriptor: TypeDescriptor<T, Specifics>, services: TypirServices<Specifics>);
dispose(): void;
protected startResolving(): void;
protected stopResolving(): void;
getType(): T | undefined;
/**
* Resolves the referenced type, if the type is not yet resolved.
* Note that the resolved type might not be completed yet.
* @returns the result of the currently executed resolution
*/
protected resolve(): 'ALREADY_RESOLVED' | 'SUCCESSFULLY_RESOLVED' | 'RESOLVING_FAILED';
addListener(listener: TypeReferenceListener<T, Specifics>, informAboutCurrentState: boolean): void;
removeListener(listener: TypeReferenceListener<T, Specifics>): void;
onAddedType(_addedType: Type, _key: string): void;
onRemovedType(removedType: Type, _key: string): void;
onAddedInferenceRule(_rule: TypeInferenceRule<Specifics>, _options: TypeInferenceRuleOptions): void;
onRemovedInferenceRule(_rule: TypeInferenceRule<Specifics>, _options: TypeInferenceRuleOptions): void;
}
//# sourceMappingURL=type-reference.d.ts.map