Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/actionify/src/deps/types.ts>UnwrapOpaque

Create and manage your GitHub workflows with TypeScript and Deno.
Latest
type alias UnwrapOpaque
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?

  1. Use an Opaque type as object keys
  2. 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

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>;

Type Parameters

OpaqueType extends Tagged<unknown>
definition: OpaqueType extends Opaque<infer Type, OpaqueType[tag]> ? Type : OpaqueType