import { type UnwrapOpaque } from "https://deno.land/x/actionify@0.3.0/src/deps/types.ts";
Revert an opaque type back to its original type by removing the readonly [tag]
.
Why is this necessary?
- Use an
Opaque
type as object keys - Prevent TS4058 error: "Return type of exported function has or is using name X from external module Y but cannot be named"
Examples
Example 1
Example 1
import type {Opaque, UnwrapOpaque} from 'type-fest';
type AccountType = Opaque<'SAVINGS' | 'CHECKING', 'AccountType'>;
const moneyByAccountType: Record<UnwrapOpaque<AccountType>, number> = {
SAVINGS: 99,
CHECKING: 0.1
};
// Without UnwrapOpaque, the following expression would throw a type error.
const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
// Attempting to pass an non-Opaque type to UnwrapOpaque will raise a type error.
type WontWork = UnwrapOpaque<string>;
definition: OpaqueType extends Opaque<infer Type, OpaqueType[tag]> ? Type : OpaqueType