Skip to main content

ErasedType Class

Erased type which can be used to expose a opaque/erased version of a type without referencing the actual type.

Sealed

This type is "sealed," meaning that code outside of the library defining it should not implement or extend it. Future versions of this type may add members or make typing of readonly members more specific.

Signature

/** @sealed */
export declare abstract class ErasedType<out Name = unknown>

Type Parameters

ParameterDefaultDescription
Nameunknown

Remarks

This similar to the type erasure pattern, but for erasing types at the package boundary.

This can be used to implement the TypeScript typing for the handle 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.

As this is a class and not just an interface, to match derived types, the declarations for any two derivatives must come from the same source - the same package version. If a type must cross package boundaries, as may be the case for cross layer types, the derived type should pick a specific version of core-interfaces to import ErasedType from. Exact versions are best, but as security best practice, use ~ specification. Consumers are expected to use a package manager that will produce consistency over minor patches. A change in version should be considered a breaking change.

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

package.json:

"dependencies": {
"@fluidframework/erased-type-v1": "npm:@fluidframework/core-interfaces@~2.0.0"
}

source.ts:

import { ErasedType as ErasedTypeV1 } from "@fluidframework/erased-type-v1";
// public sealed type
export interface ErasedMyType extends ErasedTypeV1<"myPackage.MyType"> {}
// internal type
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

MethodReturn TypeDescription
[Symbol.hasInstance](value)value is neverSince this class is a compile time only type brand, instanceof will never work with it. This Symbol.hasInstance declaration (no definition) ensures that instanceof will produce ReferenceError if used at runtime. And in TypeScript 5.3 and newer will produce a compile time error if used.

Methods

MethodReturn TypeDescription
brand(dummy)NameCompile 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 declaration (no definition) ensures that instanceof will produce ReferenceError if used at runtime. 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

ParameterTypeDescription
valuenever

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

ParameterTypeDescription
dummynever

Returns

Return type: Name