@typespec/compiler
Version:
TypeSpec Compiler Preview
100 lines • 3.68 kB
TypeScript
import { Program } from "../core/program.js";
import { Type } from "../core/types.js";
import type { Typekit } from "../typekit/index.js";
/**
* A Realm's view of a Program's state map for a given state key.
*
* For all operations, if a type was created within the realm, the realm's own state map is used. Otherwise, the owning'
* Program's state map is used.
*
* @experimental
*/
declare class StateMapRealmView<V> implements Map<Type, V> {
#private;
constructor(realm: Realm, realmState: Map<Type, V>, parentState: Map<Type, V>);
has(t: Type): boolean;
set(t: Type, v: any): this;
get(t: Type): V | undefined;
delete(t: Type): boolean;
forEach(cb: (value: V, key: Type, map: Map<Type, V>) => void, thisArg?: any): this;
get size(): number;
clear(): void;
entries(): IterableIterator<[Type, V]>;
values(): IterableIterator<V>;
keys(): IterableIterator<Type>;
[Symbol.iterator](): IterableIterator<[Type, V]>;
[Symbol.toStringTag]: string;
}
/**
* A Realm is an alternate view of a Program where types can be cloned, deleted, and modified without affecting the
* original types in the Program.
*
* The realm stores the types that exist within the realm, views of state maps that only apply within the realm,
* and a view of types that have been removed from the realm's view.
*
* @experimental
*/
export declare class Realm {
#private;
key: symbol;
/**
* Create a new realm in the given program.
*
* @param program - The program to create the realm in.
* @param description - A short description of the realm's purpose.
*/
constructor(program: Program, description: string);
/**
* The typekit instance bound to this realm.
*
* If the realm does not already have a typekit associated with it, one will be created and bound to this realm.
*/
get typekit(): Typekit;
/**
* The program that this realm is associated with.
*/
get program(): Program;
/**
* Gets a state map for the given state key symbol.
*
* This state map is a view of the program's state map for the given state key, with modifications made to the realm's
* own state.
*
* @param stateKey - The symbol to use as the state key.
* @returns The realm's state map for the given state key.
*/
stateMap(stateKey: symbol): StateMapRealmView<any>;
/**
* Clones a type and adds it to the realm. This operation will use the realm's typekit to clone the type.
*
* @param type - The type to clone.
* @returns A clone of the input type that exists within this realm.
*/
clone<T extends Type>(type: T): T;
/**
* Removes a type from this realm. This operation will not affect the type in the program, only this realm's view
* of the type.
*
* @param type - The TypeSpec type to remove from this realm.
*/
remove(type: Type): void;
/**
* Determines whether or not this realm contains a given type.
*
* @param type - The type to check.
* @returns true if the type was created within this realm or added to this realm, false otherwise.
*/
hasType(type: Type): boolean;
/**
* Adds a type to this realm. Once a type is added to the realm, the realm considers it part of itself.
*
* A type can be present in multiple realms, but `Realm.realmForType` will only return the last realm that the type
* was added to.
*
* @param type - The type to add to this realm.
*/
addType(type: Type): void;
static realmForType: WeakMap<Type, Realm>;
}
export {};
//# sourceMappingURL=realm.d.ts.map