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(testing/unstable): add type test for mutual assignability #6154

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion testing/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"./mock": "./mock.ts",
"./snapshot": "./snapshot.ts",
"./time": "./time.ts",
"./types": "./types.ts"
"./types": "./types.ts",
"./unstable-types": "./unstable_types.ts"
}
}
28 changes: 28 additions & 0 deletions testing/unstable_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/**
* Checks if the actual type `A` is assignable to the expected type `E`, and
* vice versa.
*
* This is often less strict than `IsExact` because the two type parameters are
* allowed to have a different structure as long as they are assignable to each
* other. This is often more strict than `Has` because none of the two type
* parameters may be a union that contains the other, as this would fail the
* check for mutual assignability.
*
* @example Usage
* ```ts
* import { assertType } from "@std/testing/types";
* import type { IsMutuallyAssignable } from "@std/testing/unstable-types";
*
* // false because E is not assignable to A
* assertType<IsMutuallyAssignable<string & RegExpMatchArray, string>>(false);
* // false because A is not assignable to E
* assertType<IsMutuallyAssignable<string | RegExpMatchArray, string>>(false);
* // true because both types are assignable to each other
* assertType<IsMutuallyAssignable<string | (string & RegExpMatchArray), string>>(true);
* ```
*/
export type IsMutuallyAssignable<A, E> = [E] extends [A]
? [A] extends [E] ? true : false
: false;
28 changes: 28 additions & 0 deletions testing/unstable_types_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file

import { assertType } from "./types.ts";
import type { IsMutuallyAssignable } from "./unstable_types.ts";

// IsMutuallyAssignable
{
// matching
assertType<IsMutuallyAssignable<string | (string & Date), string>>(true);
assertType<IsMutuallyAssignable<string & (string | Date), string>>(true);
assertType<
IsMutuallyAssignable<string, string | (string & RegExpMatchArray)>
>(true);
assertType<
IsMutuallyAssignable<(Date & string) | (string & Date), Date & string>
>(
true,
);
assertType<IsMutuallyAssignable<never, 0 & 1>>(true);

// not matching
assertType<IsMutuallyAssignable<string & RegExpMatchArray, string>>(false);
assertType<IsMutuallyAssignable<string | RegExpMatchArray, string>>(false);
assertType<IsMutuallyAssignable<string | number, Date>>(false);
assertType<IsMutuallyAssignable<string, number>>(false);
assertType<IsMutuallyAssignable<never, any>>(false);
}