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 1 commit
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, DeOpaque} from './source/opaque';
export {InvariantOf} from './source/invariant-of';
export {SetOptional} from './source/set-optional';
export {SetRequired} from './source/set-required';
Expand Down
2 changes: 2 additions & 0 deletions source/opaque.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ type Person = {
@category Type
*/
export type Opaque<Type, Token = unknown> = Type & Tagged<Token>;

export type DeOpaque<Type> = Omit<Type, keyof Tagged<unknown>>;
jmike marked this conversation as resolved.
Show resolved Hide resolved
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, DeOpaque} 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 = DeOpaque<Value>;
expectAssignable<PlainValue>(123);

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