ErasedType Class
Erased type which can be used to expose a opaque/erased version of a type without referencing the actual type.
Signature
/** @sealed */
export declare abstract class ErasedType<out Name = unknown>
Type Parameters
Parameter | Default | Description |
---|---|---|
Name | unknown |
Remarks
This similar to the [type erasure](https://en.wikipedia.org/wiki/Type\_erasure) pattern, but for erasing types at the package boundary.
This can be used to implement the TypeScript typing for the [handle](https://en.wikipedia.org/wiki/Handle\_(computing)) pattern, allowing code outside of a package to have a reference/handle to something in the package in a type safe way without the package having to publicly export the types of the object. This should not be confused with the more specific IFluidHandle which is also named after this design pattern.
Recommended usage is to use interface
instead of type
so tooling (such as tsc and refactoring tools) uses the type name instead of expanding it.
Example
// public
export interface ErasedMyType extends ErasedType<"myPackage.MyType"> {}
// internal
export interface MyType {
example: number;
}
// Usage
function extract(input: ErasedMyType): MyType {
return input as unknown as MyType;
}
function erase(input: MyType): ErasedMyType {
return input as unknown as ErasedMyType;
}
Do not use this class with instanceof
: this will always be false at runtime, but the compiler may think it's true in some cases.
Static Methods
Method | Return Type | Description |
---|---|---|
[Symbol.hasInstance](value) | value is never | Since this class is a compile time only type brand, instanceof will never work with it. This Symbol.hasInstance implementation ensures that instanceof will error if used, and in TypeScript 5.3 and newer will produce a compile time error if used. |
Methods
Method | Return Type | Description |
---|---|---|
brand(dummy) | Name | Compile time only marker to make type checking more strict. This method will not exist at runtime and accessing it is invalid. |
Method Details
[Symbol.hasInstance]
Since this class is a compile time only type brand, instanceof
will never work with it. This Symbol.hasInstance
implementation ensures that instanceof
will error if used, and in TypeScript 5.3 and newer will produce a compile time error if used.
Signature
static [Symbol.hasInstance](value: never): value is never;
Parameters
Parameter | Type | Description |
---|---|---|
value | never |
Returns
Return type: value is never
brand
Compile time only marker to make type checking more strict. This method will not exist at runtime and accessing it is invalid.
Signature
protected abstract brand(dummy: never): Name;
Parameters
Parameter | Type | Description |
---|---|---|
dummy | never |
Returns
Return type: Name