/* The failure messages are the names of these types. TypeScript prints the reference as is, for example `Type 'string' does not satisfy the constraint 'ActualTypeIs'`, so the message names the actual type without any string building. The names are the public wording of a failure, and runner.js reads them to describe a failure in one line. Do not rename them. The keys are symbols that are not exported, so an ordinary expected type cannot construct a matching property. Public method signatures can expose these failure types, so the check is also repeated on `this` whenever the expected type satisfies the constraint. */ declare const actualTypeIs: unique symbol; type ActualTypeIs = { readonly [actualTypeIs]: Type; }; declare const actualTypeIsIdentical: unique symbol; type ActualTypeIsIdentical = { readonly [actualTypeIsIdentical]: Type; }; declare const actualTypeIsNotAssignable: unique symbol; type ActualTypeIsNotAssignable = { readonly [actualTypeIsNotAssignable]: Type; }; declare const actualTypeIsAssignable: unique symbol; type ActualTypeIsAssignable = { readonly [actualTypeIsAssignable]: Type; }; type IsEqual = (() => Type extends Left ? true : false) extends (() => Type extends Right ? true : false) ? true : false; type IsAnyOrNever = [Type] extends [never] ? true : 0 extends 1 & Type ? true : false; // TypeScript compares `this` parameters only when both functions have one, so the `this` parameter of the function itself is compared separately. type IsIdentical = IsEqual extends true ? IsEqual, ThisParameterOf> : false; type ToBe = IsIdentical extends true ? unknown : ActualTypeIs; type NotToBe = IsIdentical extends true ? ActualTypeIsIdentical : unknown; // `any` and `never` are assignable to almost everything, which is how an accidental one passes unnoticed, so here they are only assignable to themselves. Identical types come first, because TypeScript can tell that also for a type parameter, like `this` in a class, where it cannot tell whether the type is `any` or `never`. Equality still needs an assignability check, because it does not distinguish abstract constructors from concrete ones. Comparing return types lets TypeScript resolve the check for identical type parameters too. type IsAssignable = IsEqual extends true ? (() => Actual) extends (() => Expected) ? true : false : IsAnyOrNever extends true ? false : [Actual] extends [Expected] ? true : false; type ToBeAssignableTo = IsAssignable extends true ? unknown : ActualTypeIsNotAssignable; // The check is a separate type, like `IsIdentical` in `NotToBe`, because with the failure in the true branch of `[Actual] extends [Expected]` itself, the constraint is circular and TypeScript never checks it. type NotToBeAssignableTo = IsAssignable extends true ? ActualTypeIsAssignable : unknown; // An expected type can satisfy a failed assertion's constraint, like `any`, `never`, nullable types without strictNullChecks, or an exposed failure type. Repeat that check on `this` so the expected type cannot bypass it. // A passing assertion has `unknown` as the check, which every expected type satisfies, so it is left out first. Comparing the expected type with it anyway takes a lot more memory in a large test file. type ExpectedTypeGuard = unknown extends Check ? unknown : [Expected] extends [Check] ? Check : unknown; // A `never` argument list matches every call signature, including generic rest parameters constrained to `never[]`. type ReturnTypeOf = Type extends (...arguments_: never) => infer Return ? Return : never; type ParametersOf = Type extends (...arguments_: infer Parameters) => any ? Parameters : never; // `never` for a type that is not a function, so in a union, only the functions count. The members that are not functions are left out before the `infer`, which is much slower for each member of a large union. type ThisParameterOf = Type extends (...arguments_: never) => unknown ? (Type extends (this: infer This, ...arguments_: never) => any ? This : never) : never; // Thenables do not need to return another promise from their `then` method. // eslint-disable-next-line @typescript-eslint/no-restricted-types -- Match Awaited's object guard, which includes class instances and callable objects. type ResolvedTypeOf = Type extends object & {then: (...arguments_: never) => unknown} ? Awaited : never; /* Named, so that a failure message names it instead of printing the whole signature, like `Negated` when the check is repeated on `this` because the expected type satisfies the constraint. runner.js reads the names `ExpectTypeOf` and `Negated` to tell that case apart. Do not rename them. */ type Negated = { /** Assert that the type is not identical to `Expected`. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(greet).not.toBe<(name: number) => string>(); ``` */ toBe>(this: ExpectedTypeGuard>): void; /** Assert that the type is not assignable to `Expected`. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(greet).not.toBeAssignableTo<(name: number) => unknown>(); ``` */ toBeAssignableTo>(this: ExpectedTypeGuard>): void; }; export type ExpectTypeOf = { /** Negated assertions. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(greet).not.toBe<(name: number) => string>(); ``` */ not: Negated; /** The return type of the function. For an overloaded function, this is the return type of the last overload. For a union, only its function members count. It is `unknown` for `any`, and `never` if the type is not a function. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(greet).returns.toBe(); ``` */ returns: ExpectTypeOf>; /** The parameter types of the function, as a tuple. For an overloaded function, these are the parameters of the last overload. For a union, only its function members count. It is `unknown[]` for `any`, and `never` if the type is not a function. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(greet).parameters.toBe<[name: string]>(); ``` */ parameters: ExpectTypeOf>; /** The type that a promise or thenable resolves to, recursively unwrapped using TypeScript's `Awaited` utility. For a union, like `Promise | undefined`, only its promises and thenables count. It is `any` for `any`, and `never` if the type is not a promise or thenable. For an overloaded `then` method, only the last overload counts, which can differ from the type of an `await` expression. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(fetchGreeting()).resolves.toBe(); ``` */ resolves: ExpectTypeOf>; /** Assert that the type is identical to `Expected`. `any`, `unknown`, and `never` are only identical to themselves, so `toBe()` catches an accidental `any`. `readonly` and optional modifiers must match, and so must the `this` parameter of a function. Without a type argument, the expected type is `never`, so the assertion fails for every other type. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(greet('World')).toBe(); ``` */ toBe = never>(this: ExpectedTypeGuard>): void; /** Assert that the type is assignable to `Expected`. This is the normal TypeScript assignability, except that `any` and `never` are only assignable to themselves, so an accidental `any` or `never` fails. Without a type argument, the expected type is `never`, so the assertion fails for every other type. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(greet).toBeAssignableTo<(name: string) => unknown>(); ``` */ toBeAssignableTo = never>(this: ExpectedTypeGuard>): void; }; /** Start a type assertion on the type of `value`. The assertions are only checked by the TypeScript compiler. They do nothing at runtime. @example ``` import {expectTypeOf} from 'nz'; expectTypeOf(greet('World')).toBe(); expectTypeOf(greet).returns.toBe(); expectTypeOf(greet).not.toBe<(name: number) => string>(); expectTypeOf(greet).toBeAssignableTo<(name: string) => unknown>(); ``` */ // Two overloads, not an optional `value?: Actual`, because that is `Actual | undefined`, so a value that can be `undefined` would be inferred without it. export function expectTypeOf(value: Actual): ExpectTypeOf; /** Start a type assertion on the type `Type`, for a type that has no value to pass, like a type a package exports. @example ``` import {expectTypeOf} from 'nz'; import type {IsEqual} from './index.js'; expectTypeOf>().toBe(); ``` */ export function expectTypeOf(): ExpectTypeOf; /** Assert that `value` causes a type error. It passes when there is at least one type error in its type argument or parentheses, and fails with `Expected an error, but found none.` otherwise. Any error counts, except one for a name that does not exist or an unused `@ts-expect-error` directive, which are mistakes in the test. When `tsc` skipped the type check, no error counts. With a `message`, only an error whose message includes it passes, so a test cannot pass because of a different error. Another error inside the call is reported, so the failure shows what it was. The message must be one string literal, because it is read from the source. Anything else, like a variable, is ignored, and the call then takes any error. Control characters in an unmatched message are shown as `\uXXXX` escapes. This needs the `nz` CLI, which matches type errors to the calls. The calls are found by name, so `expectError` from `tsd` works too. With plain `tsc`, use a `// @ts-expect-error` comment instead. @param value - The expression that causes the type error. @param message - A part of the error message. @example ``` import {expectError} from 'nz'; expectError(greet(1)); expectError(greet(1), 'not assignable to parameter of type'); ``` */ export function expectError(value: Expected, message?: string): void; // eslint-disable-next-line unicorn/require-module-specifiers -- Without this, a declaration file exports every declaration in it, also the symbols that the failure types are keyed by. export {};