// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright @dsherret and dsherret/conditional-type-checks contributors. All rights reserved. MIT license. /** * Asserts at compile time that the provided type argument's type resolves to the expected boolean literal type. * @param expectTrue - True if the passed in type argument resolved to true. * @example * ```typescript, ignore * import { assertType, IsExact, IsNullable } from "https://deno.land/std@$STD_VERSION/testing/types.ts"; * * const result = "some result" as string | number; * * // compile error if the type of `result` is not exactly `string | number` * assertType>(true); * * // causes a compile error that `true` is not assignable to `false` * assertType>(true); // error: string is not nullable * ``` */ export function assertType(_expectTrue: T) { } /** * Asserts at compile time that the provided type argument's type resolves to true. * * @example * ```typescript * import { AssertTrue, Has, IsNullable } from "https://deno.land/std@$STD_VERSION/testing/types.ts"; * * const result = 1 as string | number | null; * * type doTest = AssertTrue | IsNullable>; * ``` */ export type AssertTrue = never; /** * Asserts at compile time that the provided type argument's type resolves to false. * @example * ```typescript * import { AssertFalse, IsNever } from "https://deno.land/std@$STD_VERSION/testing/types.ts"; * * const result = 1 as string | number | null; * * type doTest = AssertFalse>; * ``` */ export type AssertFalse = never; /** * Asserts at compile time that the provided type argument's type resolves to the expected boolean literal type. * @example * ```typescript * import { Assert, Has } from "https://deno.land/std@$STD_VERSION/testing/types.ts"; * * const result = 1 as string | number | null; * * type doTest = Assert, true>; * ``` */ export type Assert = never; /** * Checks if type `T` has the specified type `U`. */ export type Has = IsAny extends true ? true : IsAny extends true ? false : Extract extends never ? false : true; /** * Checks if type `T` does not have the specified type `U`. */ export type NotHas = Has extends false ? true : false; /** * Checks if type `T` is possibly null or undefined. */ export type IsNullable = Extract extends never ? false : true; /** * Checks if type `T` exactly matches type `U`. */ export type IsExact = TupleMatches, AnyToBrand> extends true ? TupleMatches, DeepPrepareIsExact> extends true ? true : false : false; type DeepPrepareIsExact = { // make optional properties required [P in keyof T]-?: IsAny extends true ? AnyBrand : DeepPrepareIsExactProp; }; type DeepPrepareIsExactProp = Prop extends VisitedTypes // recursive, bail ? Prop // not recursive, keep going and add the parent type as a visited type : DeepPrepareIsExact; /** * Checks if type `T` is the `any` type. */ // https://stackoverflow.com/a/49928360/3406963 export type IsAny = 0 extends (1 & T) ? true : false; /** * Checks if type `T` is the `never` type. */ export type IsNever = [T] extends [never] ? true : false; /** * Checks if type `T` is the `unknown` type. */ export type IsUnknown = unknown extends T ? ([T] extends [null] ? false : true) : false; type TupleMatches = Matches<[T], [U]>; type Matches = T extends U ? U extends T ? true : false : false; type AnyToBrand = IsAny extends true ? AnyBrand : T; type AnyBrand = { __conditionalTypeChecksAny__: undefined };