Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce UnwrapOpaque type #403

Merged
merged 5 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export {PartialDeep} from './source/partial-deep';
export {ReadonlyDeep} from './source/readonly-deep';
export {LiteralUnion} from './source/literal-union';
export {Promisable} from './source/promisable';
export {Opaque} from './source/opaque';
export {Opaque, UnwrapOpaque} from './source/opaque';
export {InvariantOf} from './source/invariant-of';
export {SetOptional} from './source/set-optional';
export {SetRequired} from './source/set-required';
Expand Down
33 changes: 33 additions & 0 deletions source/opaque.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,36 @@ type Person = {
@category Type
*/
export type Opaque<Type, Token = unknown> = Type & Tagged<Token>;

/**
Remove readonly [tag] from an Opaque type, essentially reverting it back to a generic (i.e. non-Opaque) type.
jmike marked this conversation as resolved.
Show resolved Hide resolved

Why is this necessary?

1. Use Opaque type as object keys;
2. Prevent TS4058 error, i.e. "Return type of exported function has or is using name X from external module Y but cannot be named".

@example
```
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>;
```

@category Type
*/
export type UnwrapOpaque<OpaqueType extends Tagged<unknown>> =
OpaqueType extends Opaque<infer Type, OpaqueType[typeof tag]>
? Type
: OpaqueType;
12 changes: 10 additions & 2 deletions test-d/opaque.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectAssignable, expectError} from 'tsd';
import type {Opaque} from '../index';
import {expectAssignable, expectError, expectNotType} from 'tsd';
import type {Opaque, UnwrapOpaque} from '../index';

type Value = Opaque<number, 'Value'>;

Expand Down Expand Up @@ -38,3 +38,11 @@ const johnsId = '7dd4a16e-d5ee-454c-b1d0-71e23d9fa70b' as UUID;
// @ts-expect-error
const userJohn = userEntities[johnsId]; // eslint-disable-line @typescript-eslint/no-unused-vars
/// expectType<Foo>(userJohn);

// Remove tag from opaque value
// note: this will simply return number as type
type PlainValue = UnwrapOpaque<Value>;
expectAssignable<PlainValue>(123);

const plainValue: PlainValue = 123 as PlainValue;
expectNotType<Value>(plainValue);