Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

expect-type

Package Overview
Dependencies
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expect-type - npm Package Compare versions

Comparing version
0.19.0
to
0.20.0-0
+61
dist/branding.d.ts
import { ConstructorOverloadParameters, NumOverloads, OverloadsInfoUnion } from './overloads';
import { IsNever, IsAny, IsUnknown, ReadonlyKeys, RequiredKeys, OptionalKeys, MutuallyExtends, UnionToTuple } from './utils';
/**
* Represents a deeply branded type.
*
* Recursively walk a type and replace it with a branded type related to the
* original. This is useful for equality-checking stricter than
* `A extends B ? B extends A ? true : false : false`, because it detects the
* difference between a few edge-case types that vanilla TypeScript
* doesn't by default:
* - `any` vs `unknown`
* - `{ readonly a: string }` vs `{ a: string }`
* - `{ a?: string }` vs `{ a: string | undefined }`
*
* __Note__: not very performant for complex types - this should only be used
* when you know you need it. If doing an equality check, it's almost always
* better to use {@linkcode StrictEqualUsingTSInternalIdenticalToOperator}.
*/
export type DeepBrand<T> = IsNever<T> extends true ? {
type: 'never';
} : IsAny<T> extends true ? {
type: 'any';
} : IsUnknown<T> extends true ? {
type: 'unknown';
} : T extends string | number | boolean | symbol | bigint | null | undefined | void ? {
type: 'primitive';
value: T;
} : T extends new (...args: any[]) => any ? {
type: 'constructor';
params: ConstructorOverloadParameters<T>;
instance: DeepBrand<InstanceType<Extract<T, new (...args: any) => any>>>;
} : T extends (...args: infer P) => infer R ? NumOverloads<T> extends 1 ? {
type: 'function';
params: DeepBrand<P>;
return: DeepBrand<R>;
this: DeepBrand<ThisParameterType<T>>;
props: DeepBrand<Omit<T, keyof Function>>;
} : UnionToTuple<OverloadsInfoUnion<T>> extends infer OverloadsTuple ? {
type: 'overloads';
overloads: {
[K in keyof OverloadsTuple]: DeepBrand<OverloadsTuple[K]>;
};
} : never : T extends any[] ? {
type: 'array';
items: {
[K in keyof T]: T[K];
};
} : {
type: 'object';
properties: {
[K in keyof T]: DeepBrand<T[K]>;
};
readonly: ReadonlyKeys<T>;
required: RequiredKeys<T>;
optional: OptionalKeys<T>;
constructorParams: DeepBrand<ConstructorOverloadParameters<T>>;
};
/**
* Checks if two types are strictly equal using branding.
*/
export type StrictEqualUsingBranding<Left, Right> = MutuallyExtends<DeepBrand<Left>, DeepBrand<Right>>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
import { StrictEqualUsingBranding } from './branding';
import { And, Extends, Not, IsAny, UsefulKeys, ExtendsExcludingAnyOrNever, IsUnknown, IsNever } from './utils';
/**
* Determines the printable type representation for a given type.
*/
export type PrintType<T> = IsUnknown<T> extends true ? 'unknown' : IsNever<T> extends true ? 'never' : IsAny<T> extends true ? never : boolean extends T ? 'boolean' : T extends boolean ? `literal boolean: ${T}` : string extends T ? 'string' : T extends string ? `literal string: ${T}` : number extends T ? 'number' : T extends number ? `literal number: ${T}` : T extends null ? 'null' : T extends undefined ? 'undefined' : T extends (...args: any[]) => any ? 'function' : '...';
/**
* Helper for showing end-user a hint why their type assertion is failing.
* This swaps "leaf" types with a literal message about what the actual and expected types are.
* Needs to check for Not<IsAny<Actual>> because otherwise LeafTypeOf<Actual> returns never, which extends everything 🤔
*/
export type MismatchInfo<Actual, Expected> = And<[Extends<PrintType<Actual>, '...'>, Not<IsAny<Actual>>]> extends true ? And<[Extends<any[], Actual>, Extends<any[], Expected>]> extends true ? Array<MismatchInfo<Extract<Actual, any[]>[number], Extract<Expected, any[]>[number]>> : {
[K in UsefulKeys<Actual> | UsefulKeys<Expected>]: MismatchInfo<K extends keyof Actual ? Actual[K] : never, K extends keyof Expected ? Expected[K] : never>;
} : StrictEqualUsingBranding<Actual, Expected> extends true ? Actual : `Expected: ${PrintType<Expected>}, Actual: ${PrintType<Exclude<Actual, Expected>>}`;
declare const inverted: unique symbol;
type Inverted<T> = {
[inverted]: T;
};
declare const expectNull: unique symbol;
export type ExpectNull<T> = {
[expectNull]: T;
result: ExtendsExcludingAnyOrNever<T, null>;
};
declare const expectUndefined: unique symbol;
export type ExpectUndefined<T> = {
[expectUndefined]: T;
result: ExtendsExcludingAnyOrNever<T, undefined>;
};
declare const expectNumber: unique symbol;
export type ExpectNumber<T> = {
[expectNumber]: T;
result: ExtendsExcludingAnyOrNever<T, number>;
};
declare const expectString: unique symbol;
export type ExpectString<T> = {
[expectString]: T;
result: ExtendsExcludingAnyOrNever<T, string>;
};
declare const expectBoolean: unique symbol;
export type ExpectBoolean<T> = {
[expectBoolean]: T;
result: ExtendsExcludingAnyOrNever<T, boolean>;
};
declare const expectVoid: unique symbol;
export type ExpectVoid<T> = {
[expectVoid]: T;
result: ExtendsExcludingAnyOrNever<T, void>;
};
declare const expectFunction: unique symbol;
export type ExpectFunction<T> = {
[expectFunction]: T;
result: ExtendsExcludingAnyOrNever<T, (...args: any[]) => any>;
};
declare const expectObject: unique symbol;
export type ExpectObject<T> = {
[expectObject]: T;
result: ExtendsExcludingAnyOrNever<T, object>;
};
declare const expectArray: unique symbol;
export type ExpectArray<T> = {
[expectArray]: T;
result: ExtendsExcludingAnyOrNever<T, any[]>;
};
declare const expectSymbol: unique symbol;
export type ExpectSymbol<T> = {
[expectSymbol]: T;
result: ExtendsExcludingAnyOrNever<T, symbol>;
};
declare const expectAny: unique symbol;
export type ExpectAny<T> = {
[expectAny]: T;
result: IsAny<T>;
};
declare const expectUnknown: unique symbol;
export type ExpectUnknown<T> = {
[expectUnknown]: T;
result: IsUnknown<T>;
};
declare const expectNever: unique symbol;
export type ExpectNever<T> = {
[expectNever]: T;
result: IsNever<T>;
};
declare const expectNullable: unique symbol;
export type ExpectNullable<T> = {
[expectNullable]: T;
result: Not<StrictEqualUsingBranding<T, NonNullable<T>>>;
};
/**
* Checks if the result of an expecter matches the specified options, and resolves to a fairly readable error messsage if not.
*/
export type Scolder<Expecter extends {
result: boolean;
}, Options extends {
positive: boolean;
}> = Expecter['result'] extends Options['positive'] ? () => true : Options['positive'] extends true ? Expecter : Inverted<Expecter>;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const inverted = Symbol('inverted');
const expectNull = Symbol('expectNull');
const expectUndefined = Symbol('expectUndefined');
const expectNumber = Symbol('expectNumber');
const expectString = Symbol('expectString');
const expectBoolean = Symbol('expectBoolean');
const expectVoid = Symbol('expectVoid');
const expectFunction = Symbol('expectFunction');
const expectObject = Symbol('expectObject');
const expectArray = Symbol('expectArray');
const expectSymbol = Symbol('expectSymbol');
const expectAny = Symbol('expectAny');
const expectUnknown = Symbol('expectUnknown');
const expectNever = Symbol('expectNever');
const expectNullable = Symbol('expectNullable');
import { StrictEqualUsingTSInternalIdenticalToOperator, IsNever, UnionToIntersection, UnionToTuple } from './utils';
/**
* The simple(ish) way to get overload info from a function `F`. Recent versions of TypeScript will match any function against a generic 10-overload type, filling in slots with duplicates of the function.
* So, we can just match against a single type and get all the overloads.
*
* For older versions of TypeScript, we'll need to painstakingly do ten separate matches.
*/
export type TSPost53OverloadsInfoUnion<F> = F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
(...args: infer A8): infer R8;
(...args: infer A9): infer R9;
(...args: infer A10): infer R10;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) | ((...p: A8) => R8) | ((...p: A9) => R9) | ((...p: A10) => R10) : never;
export type UnknownFn = (...p: unknown[]) => unknown;
/**
* `true` iff `T` is equivalent to `(...p: unknown[]) => unknown`, which is what an overload variant looks like for a non-existent overload
* This is useful because older versions of TypeScript end up with 9 "useless" overloads and one real one for parameterless/generic functions.
*
* Related: https://github.com/microsoft/TypeScript/issues/28867
*/
export type IsUselessOverloadInfo<T> = StrictEqualUsingTSInternalIdenticalToOperator<T, UnknownFn>;
/**
* Old versions of TypeScript can sometimes seem to refuse to separate out union members unless you put them each in a pointless tuple and add an extra `infer X` expression.
* There may be a better way to work around this problem, but since it's not a problem in newer versions of TypeScript, it's not a priority right now.
*/
export type Tuplify<T> = T extends infer X ? [X] : never;
/**
* For older versions of TypeScript, we need two separate workarounds to get overload info.
* First, we need need to use {@linkcode DecreasingOverloadsInfoUnion} to get the overload info for functions with 1-10 overloads.
* Then, we need to filter out the "useless" overloads that are present in older versions of TypeScript, for parameterless functions.
* To do this we use {@linkcode IsUselessOverloadInfo} to remove useless overloads.
*
* Related: https://github.com/microsoft/TypeScript/issues/28867
*/
export type TSPre53OverloadsInfoUnion<F> = Tuplify<DecreasingOverloadsInfoUnion<F>> extends infer Tup ? Tup extends [infer Fn] ? IsUselessOverloadInfo<Fn> extends true ? never : Fn : never : never;
/**
* For versions of TypeScript below 5.3, we need to check for 10 overloads, then 9, then 8, etc., to get a union of the overlaod variants.
*/
export type DecreasingOverloadsInfoUnion<F> = F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
(...args: infer A8): infer R8;
(...args: infer A9): infer R9;
(...args: infer A10): infer R10;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) | ((...p: A8) => R8) | ((...p: A9) => R9) | ((...p: A10) => R10) : F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
(...args: infer A8): infer R8;
(...args: infer A9): infer R9;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) | ((...p: A8) => R8) | ((...p: A9) => R9) : F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
(...args: infer A8): infer R8;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) | ((...p: A8) => R8) : F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) : F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) : F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) : F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) : F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) : F extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
} ? ((...p: A1) => R1) | ((...p: A2) => R2) : F extends (...args: infer A1) => infer R1 ? ((...p: A1) => R1) : never;
/**
* Get a union of overload variants for a function `F`. Does a check for whether we can do the one-shot 10-overload matcher (which works for ts>5.3), and if not, falls back to the more complicated utility.
*/
export type OverloadsInfoUnion<F> = IsNever<TSPost53OverloadsInfoUnion<(a: 1) => 2>> extends true ? TSPre53OverloadsInfoUnion<F> : TSPost53OverloadsInfoUnion<F>;
/** Allows inferring any function using the `infer` keyword */
export type InferFn<F extends (...args: any) => any> = F;
/** A union type of the parameters allowed for any overload of function `F` */
export type OverloadParameters<F> = OverloadsInfoUnion<F> extends InferFn<infer Fn> ? Parameters<Fn> : never;
/** A union type of the return types for any overload of function `F` */
export type OverloadReturnTypes<F> = OverloadsInfoUnion<F> extends InferFn<infer Fn> ? ReturnType<Fn> : never;
/** Takes an overload variants `Union`, produced from {@linkcode OverloadsInfoUnion} and rejects the ones incompatible with parameters `A` */
export type SelectOverloadsInfo<Union extends UnknownFn, A extends unknown[]> = Union extends InferFn<infer Fn> ? (A extends Parameters<Fn> ? Fn : never) : never;
/** Creates a new overload (an intersection type) from an existing one, which only includes variant(s) which can accept `A` as parameters */
export type OverloadsNarrowedByParameters<F, A extends OverloadParameters<F>> = UnionToIntersection<SelectOverloadsInfo<OverloadsInfoUnion<F>, A>>;
/**
* The simple(ish) way to get overload info from a constructor `C`. Recent versions of TypeScript will match any constructor against a generic 10-overload type, filling in slots with duplicates of the constructor.
* So, we can just match against a single type and get all the overloads.
*
* For older versions of TypeScript, we'll need to painstakingly do ten separate matches.
*/
export type TSPost53ConstructorOverloadsInfoUnion<C> = C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
new (...args: infer A4): infer R4;
new (...args: infer A5): infer R5;
new (...args: infer A6): infer R6;
new (...args: infer A7): infer R7;
new (...args: infer A8): infer R8;
new (...args: infer A9): infer R9;
new (...args: infer A10): infer R10;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) | (new (...p: A8) => R8) | (new (...p: A9) => R9) | (new (...p: A10) => R10) : never;
export type UnknownConstructor = new (...args: unknown[]) => unknown;
export type IsUselessConstructorOverloadInfo<T> = StrictEqualUsingTSInternalIdenticalToOperator<T, UnknownConstructor>;
/**
* For older versions of TypeScript, we need two separate workarounds to get constructor overload info.
* First, we need need to use {@linkcode DecreasingConstructorOverloadsInfoUnion} to get the overload info for constructors with 1-10 overloads.
* Then, we need to filter out the "useless" overloads that are present in older versions of TypeScript, for parameterless constructors.
* To do this we use {@linkcode IsUselessConstructorOverloadInfo} to remove useless overloads.
*
* Related: https://github.com/microsoft/TypeScript/issues/28867
*/
export type TSPre53ConstructorOverloadsInfoUnion<C> = Tuplify<DecreasingConstructorOverloadsInfoUnion<C>> extends infer Tup ? Tup extends [infer Ctor] ? IsUselessConstructorOverloadInfo<Ctor> extends true ? never : Ctor : never : never;
/**
* For versions of TypeScript below 5.3, we need to check for 10 overloads, then 9, then 8, etc., to get a union of the overlaod variants.
*/
export type DecreasingConstructorOverloadsInfoUnion<C> = C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
new (...args: infer A4): infer R4;
new (...args: infer A5): infer R5;
new (...args: infer A6): infer R6;
new (...args: infer A7): infer R7;
new (...args: infer A8): infer R8;
new (...args: infer A9): infer R9;
new (...args: infer A10): infer R10;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) | (new (...p: A8) => R8) | (new (...p: A9) => R9) | (new (...p: A10) => R10) : C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
new (...args: infer A4): infer R4;
new (...args: infer A5): infer R5;
new (...args: infer A6): infer R6;
new (...args: infer A7): infer R7;
new (...args: infer A8): infer R8;
new (...args: infer A9): infer R9;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) | (new (...p: A8) => R8) | (new (...p: A9) => R9) : C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
new (...args: infer A4): infer R4;
new (...args: infer A5): infer R5;
new (...args: infer A6): infer R6;
new (...args: infer A7): infer R7;
new (...args: infer A8): infer R8;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) | (new (...p: A8) => R8) : C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
new (...args: infer A4): infer R4;
new (...args: infer A5): infer R5;
new (...args: infer A6): infer R6;
new (...args: infer A7): infer R7;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) : C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
new (...args: infer A4): infer R4;
new (...args: infer A5): infer R5;
new (...args: infer A6): infer R6;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) : C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
new (...args: infer A4): infer R4;
new (...args: infer A5): infer R5;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) : C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
new (...args: infer A4): infer R4;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) : C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
new (...args: infer A3): infer R3;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) : C extends {
new (...args: infer A1): infer R1;
new (...args: infer A2): infer R2;
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) : C extends new (...args: infer A1) => infer R1 ? (new (...p: A1) => R1) : never;
/**
* Get a union of overload variants for a constructor `C`. Does a check for whether we can do the one-shot 10-overload matcher (which works for ts>5.3), and if not, falls back to the more complicated utility.
*/
export type ConstructorOverloadsUnion<C> = IsNever<TSPost53ConstructorOverloadsInfoUnion<new (a: 1) => any>> extends true ? TSPre53ConstructorOverloadsInfoUnion<C> : TSPost53ConstructorOverloadsInfoUnion<C>;
/** Allows inferring any constructor using the `infer` keyword. */
export type InferConstructor<C extends new (...args: any) => any> = C;
/** A union type of the parameters allowed for any overload of constructor `C` */
export type ConstructorOverloadParameters<C> = ConstructorOverloadsUnion<C> extends InferConstructor<infer Ctor> ? ConstructorParameters<Ctor> : never;
export type NumOverloads<T> = UnionToTuple<OverloadsInfoUnion<T>>['length'];
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Negates a boolean type.
*/
export type Not<T extends boolean> = T extends true ? false : true;
/**
* Returns `true` if at least one of the types in the
* {@linkcode Types} array is `true`, otherwise returns `false`.
*/
export type Or<Types extends boolean[]> = Types[number] extends false ? false : true;
/**
* Checks if all the boolean types in the {@linkcode Types} array are `true`.
*/
export type And<Types extends boolean[]> = Types[number] extends true ? true : false;
/**
* Represents an equality type that returns {@linkcode Right} if
* {@linkcode Left} is `true`,
* otherwise returns the negation of {@linkcode Right}.
*/
export type Eq<Left extends boolean, Right extends boolean> = Left extends true ? Right : Not<Right>;
/**
* Represents the exclusive OR operation on a tuple of boolean types.
* Returns `true` if exactly one of the boolean types is `true`,
* otherwise returns `false`.
*/
export type Xor<Types extends [boolean, boolean]> = Not<Eq<Types[0], Types[1]>>;
declare const secret: unique symbol;
type Secret = typeof secret;
/**
* Checks if the given type is `never`.
*/
export type IsNever<T> = [T] extends [never] ? true : false;
/**
* Checks if the given type is `any`.
*/
export type IsAny<T> = [T] extends [Secret] ? Not<IsNever<T>> : false;
/**
* Determines if the given type is `unknown`.
*/
export type IsUnknown<T> = [unknown] extends [T] ? Not<IsAny<T>> : false;
/**
* Determines if a type is either `never` or `any`.
*/
export type IsNeverOrAny<T> = Or<[IsNever<T>, IsAny<T>]>;
/**
* Subjective "useful" keys from a type. For objects it's just `keyof` but for
* tuples/arrays it's the number keys.
*
* @example
* ```ts
* UsefulKeys<{a: 1; b: 2}> // 'a' | 'b'
*
* UsefulKeys<['a', 'b']> // '0' | '1'
*
* UsefulKeys<string[]> // number
* ```
*/
export type UsefulKeys<T> = T extends any[] ? {
[K in keyof T]: K;
}[number] : keyof T;
/**
* Extracts the keys from a type that are required (not optional).
*/
export type RequiredKeys<T> = Extract<{
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
}[keyof T], keyof T>;
/**
* Gets the keys of an object type that are optional.
*/
export type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>;
/**
* Extracts the keys from a type that are not readonly.
*/
export type ReadonlyKeys<T> = Extract<{
[K in keyof T]-?: ReadonlyEquivalent<{
[_K in K]: T[K];
}, {
-readonly [_K in K]: T[K];
}> extends true ? never : K;
}[keyof T], keyof T>;
/**
* Determines if two types, are equivalent in a `readonly` manner.
*/
type ReadonlyEquivalent<X, Y> = Extends<(<T>() => T extends X ? true : false), (<T>() => T extends Y ? true : false)>;
/**
* Checks if one type extends another.
*/
export type Extends<L, R> = IsNever<L> extends true ? IsNever<R> : [L] extends [R] ? true : false;
export type ExtendsExcludingAnyOrNever<L, R> = IsAny<L> extends true ? IsAny<R> : Extends<L, R>;
/**
* Checks if two types are strictly equal using
* the TypeScript internal identical-to operator.
*
* @see {@link https://github.com/microsoft/TypeScript/issues/55188#issuecomment-1656328122 much history}
*/
export type StrictEqualUsingTSInternalIdenticalToOperator<L, R> = (<T>() => T extends (L & T) | T ? true : false) extends <T>() => T extends (R & T) | T ? true : false ? IsNever<L> extends IsNever<R> ? true : false : false;
/**
* Checks that `L` and `R` extend each other. Not quite the same as an equality check since `any` can make it resolve to true.
* So should only be used when `L` and `R` are known to avoid `any`.
*/
export type MutuallyExtends<L, R> = And<[Extends<L, R>, Extends<R, L>]>;
declare const mismatch: unique symbol;
type Mismatch = {
[mismatch]: 'mismatch';
};
/**
* A type which should match anything passed as a value but *doesn't*
* match {@linkcode Mismatch}. It helps TypeScript select the right overload
* for {@linkcode PositiveExpectTypeOf.toEqualTypeOf `.toEqualTypeOf()`} and
* {@linkcode PositiveExpectTypeOf.toMatchTypeOf `.toMatchTypeOf()`}.
*/
declare const avalue: unique symbol;
/**
* Represents a value that can be of various types.
*/
export type AValue = {
[avalue]?: undefined;
} | string | number | boolean | symbol | bigint | null | undefined | void;
/**
* Represents the type of mismatched arguments between
* the actual result and the expected result.
*
* If {@linkcode ActualResult} and {@linkcode ExpectedResult} are equivalent,
* the type resolves to an empty tuple `[]`, indicating no mismatch.
* If they are not equivalent, it resolves to a tuple containing the element
* {@linkcode Mismatch}, signifying a discrepancy between
* the expected and actual results.
*/
export type MismatchArgs<ActualResult extends boolean, ExpectedResult extends boolean> = Eq<ActualResult, ExpectedResult> extends true ? [] : [Mismatch];
/**
* Represents the options for the {@linkcode ExpectTypeOf} function.
*/
export interface ExpectTypeOfOptions {
positive: boolean;
branded: boolean;
}
/** `A | B | C` -> `A & B & C` */
export type UnionToIntersection<T> = (T extends any ? (x: T) => void : never) extends (x: infer I) => void ? I : never;
/**
* Get the last element of a union. First, converts to a union of `() => T` functions, then uses `UnionToIntersection` to get the last one.
*/
export type LastOf<T> = UnionToIntersection<T extends any ? () => T : never> extends () => infer R ? R : never;
/** Intermediate type for {@linkcode UnionToTuple} which pushes the "last" union member to the end of a tuple, and recursively prepends the remainder of the union */
export type TuplifyUnion<T, L = LastOf<T>> = IsNever<T> extends true ? [] : [...TuplifyUnion<Exclude<T, L>>, L];
/** Convert a union like `1 | 2 | 3` to a tuple like `[1, 2, 3]` */
export type UnionToTuple<T> = TuplifyUnion<T>;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const secret = Symbol('secret');
const mismatch = Symbol('mismatch');
/**
* A type which should match anything passed as a value but *doesn't*
* match {@linkcode Mismatch}. It helps TypeScript select the right overload
* for {@linkcode PositiveExpectTypeOf.toEqualTypeOf `.toEqualTypeOf()`} and
* {@linkcode PositiveExpectTypeOf.toMatchTypeOf `.toMatchTypeOf()`}.
*/
const avalue = Symbol('avalue');
+18
-306

@@ -0,298 +1,9 @@

import { StrictEqualUsingBranding } from './branding';
import { MismatchInfo, Scolder, ExpectAny, ExpectUnknown, ExpectNever, ExpectFunction, ExpectObject, ExpectArray, ExpectNumber, ExpectString, ExpectBoolean, ExpectVoid, ExpectSymbol, ExpectNull, ExpectUndefined, ExpectNullable } from './messages';
import { ConstructorOverloadParameters, OverloadParameters, OverloadReturnTypes, OverloadsNarrowedByParameters } from './overloads';
import { StrictEqualUsingTSInternalIdenticalToOperator, AValue, MismatchArgs, Extends } from './utils';
export * from './branding';
export * from './utils';
export * from './messages';
/**
* Negates a boolean type.
*/
export declare type Not<T extends boolean> = T extends true ? false : true;
/**
* Returns `true` if at least one of the types in the
* {@linkcode Types} array is `true`, otherwise returns `false`.
*/
export declare type Or<Types extends boolean[]> = Types[number] extends false ? false : true;
/**
* Checks if all the boolean types in the {@linkcode Types} array are `true`.
*/
export declare type And<Types extends boolean[]> = Types[number] extends true ? true : false;
/**
* Represents an equality type that returns {@linkcode Right} if
* {@linkcode Left} is `true`,
* otherwise returns the negation of {@linkcode Right}.
*/
export declare type Eq<Left extends boolean, Right extends boolean> = Left extends true ? Right : Not<Right>;
/**
* Represents the exclusive OR operation on a tuple of boolean types.
* Returns `true` if exactly one of the boolean types is `true`,
* otherwise returns `false`.
*/
export declare type Xor<Types extends [boolean, boolean]> = Not<Eq<Types[0], Types[1]>>;
declare const secret: unique symbol;
declare type Secret = typeof secret;
/**
* Checks if the given type is `never`.
*/
export declare type IsNever<T> = [T] extends [never] ? true : false;
/**
* Checks if the given type is `any`.
*/
export declare type IsAny<T> = [T] extends [Secret] ? Not<IsNever<T>> : false;
/**
* Determines if the given type is `unknown`.
*/
export declare type IsUnknown<T> = [unknown] extends [T] ? Not<IsAny<T>> : false;
/**
* Determines if a type is either `never` or `any`.
*/
export declare type IsNeverOrAny<T> = Or<[IsNever<T>, IsAny<T>]>;
/**
* Determines the printable type representation for a given type.
*/
export declare type PrintType<T> = IsUnknown<T> extends true ? 'unknown' : IsNever<T> extends true ? 'never' : IsAny<T> extends true ? never : boolean extends T ? 'boolean' : T extends boolean ? `literal boolean: ${T}` : string extends T ? 'string' : T extends string ? `literal string: ${T}` : number extends T ? 'number' : T extends number ? `literal number: ${T}` : T extends null ? 'null' : T extends undefined ? 'undefined' : T extends (...args: any[]) => any ? 'function' : '...';
/**
* Subjective "useful" keys from a type. For objects it's just `keyof` but for
* tuples/arrays it's the number keys.
*
* @example
* ```ts
* UsefulKeys<{a: 1; b: 2}> // 'a' | 'b'
*
* UsefulKeys<['a', 'b']> // '0' | '1'
*
* UsefulKeys<string[]> // number
* ```
*/
export declare type UsefulKeys<T> = T extends any[] ? {
[K in keyof T]: K;
}[number] : keyof T;
export declare type MismatchInfo<Actual, Expected> = And<[Extends<PrintType<Actual>, '...'>, Not<IsAny<Actual>>]> extends true ? And<[Extends<any[], Actual>, Extends<any[], Expected>]> extends true ? Array<MismatchInfo<Extract<Actual, any[]>[number], Extract<Expected, any[]>[number]>> : {
[K in UsefulKeys<Actual> | UsefulKeys<Expected>]: MismatchInfo<K extends keyof Actual ? Actual[K] : never, K extends keyof Expected ? Expected[K] : never>;
} : StrictEqualUsingBranding<Actual, Expected> extends true ? Actual : `Expected: ${PrintType<Expected>}, Actual: ${PrintType<Exclude<Actual, Expected>>}`;
/**
* Represents a deeply branded type.
*
* Recursively walk a type and replace it with a branded type related to the
* original. This is useful for equality-checking stricter than
* `A extends B ? B extends A ? true : false : false`, because it detects the
* difference between a few edge-case types that vanilla typescript
* doesn't by default:
* - `any` vs `unknown`
* - `{ readonly a: string }` vs `{ a: string }`
* - `{ a?: string }` vs `{ a: string | undefined }`
*
* __Note__: not very performant for complex types - this should only be used
* when you know you need it. If doing an equality check, it's almost always
* better to use {@linkcode StrictEqualUsingTSInternalIdenticalToOperator}.
*/
export declare type DeepBrand<T> = IsNever<T> extends true ? {
type: 'never';
} : IsAny<T> extends true ? {
type: 'any';
} : IsUnknown<T> extends true ? {
type: 'unknown';
} : T extends string | number | boolean | symbol | bigint | null | undefined | void ? {
type: 'primitive';
value: T;
} : T extends new (...args: any[]) => any ? {
type: 'constructor';
params: ConstructorParams<T>;
instance: DeepBrand<InstanceType<Extract<T, new (...args: any) => any>>>;
} : T extends (...args: infer P) => infer R ? {
type: 'function';
params: DeepBrand<P>;
return: DeepBrand<R>;
this: DeepBrand<ThisParameterType<T>>;
props: DeepBrand<Omit<T, keyof Function>>;
} : T extends any[] ? {
type: 'array';
items: {
[K in keyof T]: T[K];
};
} : {
type: 'object';
properties: {
[K in keyof T]: DeepBrand<T[K]>;
};
readonly: ReadonlyKeys<T>;
required: RequiredKeys<T>;
optional: OptionalKeys<T>;
constructorParams: DeepBrand<ConstructorParams<T>>;
};
/**
* Extracts the keys from a type that are required (not optional).
*/
export declare type RequiredKeys<T> = Extract<{
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
}[keyof T], keyof T>;
/**
* Gets the keys of an object type that are optional.
*/
export declare type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>;
/**
* Extracts the keys from a type that are not readonly.
*/
export declare type ReadonlyKeys<T> = Extract<{
[K in keyof T]-?: ReadonlyEquivalent<{
[_K in K]: T[K];
}, {
-readonly [_K in K]: T[K];
}> extends true ? never : K;
}[keyof T], keyof T>;
/**
* Determines if two types, are equivalent in a `readonly` manner.
*/
declare type ReadonlyEquivalent<X, Y> = Extends<(<T>() => T extends X ? true : false), (<T>() => T extends Y ? true : false)>;
/**
* Checks if one type extends another.
*/
export declare type Extends<L, R> = IsNever<L> extends true ? IsNever<R> : [L] extends [R] ? true : false;
export declare type ExtendsUsingBranding<L, R> = Extends<DeepBrand<L>, DeepBrand<R>>;
export declare type ExtendsExcludingAnyOrNever<L, R> = IsAny<L> extends true ? IsAny<R> : Extends<L, R>;
/**
* Checks if two types are strictly equal using
* the TypeScript internal identical-to operator.
*
* @see {@link https://github.com/microsoft/TypeScript/issues/55188#issuecomment-1656328122 much history}
*/
declare type StrictEqualUsingTSInternalIdenticalToOperator<L, R> = (<T>() => T extends (L & T) | T ? true : false) extends <T>() => T extends (R & T) | T ? true : false ? IsNever<L> extends IsNever<R> ? true : false : false;
/**
* Checks if two types are strictly equal using branding.
*/
export declare type StrictEqualUsingBranding<Left, Right> = And<[
ExtendsUsingBranding<Left, Right>,
ExtendsUsingBranding<Right, Left>
]>;
/**
* Represents a type that checks if two types are equal, using
* a hopefully performant approach.
* It first checks if the types are strictly equal using
* {@linkcode StrictEqualUsingTSInternalIdenticalToOperator}.
* If they are not strictly equal, it falls back to using the
* {@linkcode StrictEqualUsingBranding} type.
*/
export declare type HopefullyPerformantEqual<Left, Right> = StrictEqualUsingTSInternalIdenticalToOperator<Left, Right> extends true ? true : StrictEqualUsingBranding<Left, Right>;
/**
* Extracts the parameter types from a function type.
*/
export declare type Params<Actual> = Actual extends (...args: infer ParameterTypes) => any ? ParameterTypes : never;
/**
* Represents the constructor parameters of a class or constructor function.
* If the constructor takes no arguments, an empty array is returned.
*/
export declare type ConstructorParams<Actual> = Actual extends new (...args: infer P) => any ? Actual extends new () => any ? P | [] : P : never;
declare const mismatch: unique symbol;
declare type Mismatch = {
[mismatch]: 'mismatch';
};
/**
* A type which should match anything passed as a value but *doesn't*
* match {@linkcode Mismatch}. It helps TypeScript select the right overload
* for {@linkcode PositiveExpectTypeOf.toEqualTypeOf `.toEqualTypeOf()`} and
* {@linkcode PositiveExpectTypeOf.toMatchTypeOf `.toMatchTypeOf()`}.
*/
declare const avalue: unique symbol;
/**
* Represents a value that can be of various types.
*/
declare type AValue = {
[avalue]?: undefined;
} | string | number | boolean | symbol | bigint | null | undefined | void;
/**
* Represents the type of mismatched arguments between
* the actual result and the expected result.
*
* If {@linkcode ActualResult} and {@linkcode ExpectedResult} are equivalent,
* the type resolves to an empty tuple `[]`, indicating no mismatch.
* If they are not equivalent, it resolves to a tuple containing the element
* {@linkcode Mismatch}, signifying a discrepancy between
* the expected and actual results.
*/
declare type MismatchArgs<ActualResult extends boolean, ExpectedResult extends boolean> = Eq<ActualResult, ExpectedResult> extends true ? [] : [Mismatch];
/**
* Represents the options for the {@linkcode ExpectTypeOf} function.
*/
export interface ExpectTypeOfOptions {
positive: boolean;
branded: boolean;
}
declare const inverted: unique symbol;
declare type Inverted<T> = {
[inverted]: T;
};
declare const expectNull: unique symbol;
declare type ExpectNull<T> = {
[expectNull]: T;
result: ExtendsExcludingAnyOrNever<T, null>;
};
declare const expectUndefined: unique symbol;
declare type ExpectUndefined<T> = {
[expectUndefined]: T;
result: ExtendsExcludingAnyOrNever<T, undefined>;
};
declare const expectNumber: unique symbol;
declare type ExpectNumber<T> = {
[expectNumber]: T;
result: ExtendsExcludingAnyOrNever<T, number>;
};
declare const expectString: unique symbol;
declare type ExpectString<T> = {
[expectString]: T;
result: ExtendsExcludingAnyOrNever<T, string>;
};
declare const expectBoolean: unique symbol;
declare type ExpectBoolean<T> = {
[expectBoolean]: T;
result: ExtendsExcludingAnyOrNever<T, boolean>;
};
declare const expectVoid: unique symbol;
declare type ExpectVoid<T> = {
[expectVoid]: T;
result: ExtendsExcludingAnyOrNever<T, void>;
};
declare const expectFunction: unique symbol;
declare type ExpectFunction<T> = {
[expectFunction]: T;
result: ExtendsExcludingAnyOrNever<T, (...args: any[]) => any>;
};
declare const expectObject: unique symbol;
declare type ExpectObject<T> = {
[expectObject]: T;
result: ExtendsExcludingAnyOrNever<T, object>;
};
declare const expectArray: unique symbol;
declare type ExpectArray<T> = {
[expectArray]: T;
result: ExtendsExcludingAnyOrNever<T, any[]>;
};
declare const expectSymbol: unique symbol;
declare type ExpectSymbol<T> = {
[expectSymbol]: T;
result: ExtendsExcludingAnyOrNever<T, symbol>;
};
declare const expectAny: unique symbol;
declare type ExpectAny<T> = {
[expectAny]: T;
result: IsAny<T>;
};
declare const expectUnknown: unique symbol;
declare type ExpectUnknown<T> = {
[expectUnknown]: T;
result: IsUnknown<T>;
};
declare const expectNever: unique symbol;
declare type ExpectNever<T> = {
[expectNever]: T;
result: IsNever<T>;
};
declare const expectNullable: unique symbol;
declare type ExpectNullable<T> = {
[expectNullable]: T;
result: Not<StrictEqualUsingBranding<T, NonNullable<T>>>;
};
/**
* Represents a scolder function that checks if the result of an expecter
* matches the specified options.
*/
declare type Scolder<Expecter extends {
result: boolean;
}, Options extends {
positive: boolean;
}> = Expecter['result'] extends Options['positive'] ? () => true : Options['positive'] extends true ? Expecter : Inverted<Expecter>;
/**
* Represents the positive assertion methods available for type checking in the

@@ -645,3 +356,3 @@ * {@linkcode expectTypeOf()} utility.

*/
export declare type ExpectTypeOf<Actual, Options extends {
export type ExpectTypeOf<Actual, Options extends {
positive: boolean;

@@ -717,3 +428,3 @@ }> = Options['positive'] extends true ? PositiveExpectTypeOf<Actual> : NegativeExpectTypeOf<Actual>;

* __Note__: You cannot negate this assertion with
* {@linkcode PositiveExpectTypeOf.not `.not`} you need to use
* {@linkcode PositiveExpectTypeOf.not `.not`}, you need to use
* `ts-expect-error` instead.

@@ -735,3 +446,3 @@ *

*/
toBeCallableWith: Options['positive'] extends true ? (...args: Params<Actual>) => true : never;
toBeCallableWith: Options['positive'] extends true ? <A extends OverloadParameters<Actual>>(...args: A) => ExpectTypeOf<OverloadsNarrowedByParameters<Actual, A>, Options> : never;
/**

@@ -754,3 +465,3 @@ * Checks whether a class is constructible with the given parameters.

*/
toBeConstructibleWith: Options['positive'] extends true ? (...args: ConstructorParams<Actual>) => true : never;
toBeConstructibleWith: Options['positive'] extends true ? <A extends ConstructorOverloadParameters<Actual>>(...args: A) => true : never;
/**

@@ -876,3 +587,3 @@ * Equivalent to the {@linkcode Extract} utility type.

*/
parameter: <Index extends keyof Params<Actual>>(index: Index) => ExpectTypeOf<Params<Actual>[Index], Options>;
parameter: <Index extends number>(index: Index) => ExpectTypeOf<OverloadParameters<Actual>[Index], Options>;
/**

@@ -894,3 +605,3 @@ * Equivalent to the {@linkcode Parameters} utility type.

*/
parameters: ExpectTypeOf<Params<Actual>, Options>;
parameters: ExpectTypeOf<OverloadParameters<Actual>, Options>;
/**

@@ -901,2 +612,4 @@ * Equivalent to the {@linkcode ConstructorParameters} utility type.

*
* For overloaded constructors it will return a union of all possible parameter-tuples.
*
* @example

@@ -909,3 +622,3 @@ * ```ts

*/
constructorParameters: ExpectTypeOf<ConstructorParams<Actual>, Options>;
constructorParameters: ExpectTypeOf<ConstructorOverloadParameters<Actual>, Options>;
/**

@@ -947,3 +660,3 @@ * Equivalent to the {@linkcode ThisParameterType} utility type.

*/
returns: Actual extends (...args: any[]) => infer R ? ExpectTypeOf<R, Options> : never;
returns: Actual extends Function ? ExpectTypeOf<OverloadReturnTypes<Actual>, Options> : never;
/**

@@ -1021,3 +734,3 @@ * Extracts resolved value of a Promise,

*/
export declare type _ExpectTypeOf = {
export type _ExpectTypeOf = {
/**

@@ -1066,2 +779,1 @@ * Asserts the expected type of a value.

export declare const expectTypeOf: _ExpectTypeOf;
export {};
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.expectTypeOf = void 0;
const secret = Symbol('secret');
const mismatch = Symbol('mismatch');
/**
* A type which should match anything passed as a value but *doesn't*
* match {@linkcode Mismatch}. It helps TypeScript select the right overload
* for {@linkcode PositiveExpectTypeOf.toEqualTypeOf `.toEqualTypeOf()`} and
* {@linkcode PositiveExpectTypeOf.toMatchTypeOf `.toMatchTypeOf()`}.
*/
const avalue = Symbol('avalue');
const inverted = Symbol('inverted');
const expectNull = Symbol('expectNull');
const expectUndefined = Symbol('expectUndefined');
const expectNumber = Symbol('expectNumber');
const expectString = Symbol('expectString');
const expectBoolean = Symbol('expectBoolean');
const expectVoid = Symbol('expectVoid');
const expectFunction = Symbol('expectFunction');
const expectObject = Symbol('expectObject');
const expectArray = Symbol('expectArray');
const expectSymbol = Symbol('expectSymbol');
const expectAny = Symbol('expectAny');
const expectUnknown = Symbol('expectUnknown');
const expectNever = Symbol('expectNever');
const expectNullable = Symbol('expectNullable');
__exportStar(require("./branding"), exports); // backcompat, consider removing in next major version
__exportStar(require("./utils"), exports); // backcompat, consider removing in next major version
__exportStar(require("./messages"), exports); // backcompat, consider removing in next major version
const fn = () => true;

@@ -83,5 +76,4 @@ /**

toEqualTypeOf: fn,
toBeCallableWith: fn,
toBeConstructibleWith: fn,
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
toBeCallableWith: exports.expectTypeOf,
extract: exports.expectTypeOf,

@@ -88,0 +80,0 @@ exclude: exports.expectTypeOf,

+15
-25

@@ -0,1 +1,16 @@

Copyright 2024 Misha Kaletsky
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apache License

@@ -177,26 +192,1 @@ Version 2.0, January 2004

END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
{
"name": "expect-type",
"version": "0.19.0",
"version": "0.20.0-0",
"engines": {
"node": ">=12.0.0"
},
"packageManager": "pnpm@8.10.2",
"keywords": [

@@ -30,10 +29,11 @@ "typescript",

"devDependencies": {
"@types/node": "^14.0.0",
"@types/node": "^20.0.0",
"eslint": "^8.57.0",
"eslint-plugin-mmkal": "0.4.1",
"eslint-plugin-mmkal": "0.7.0",
"np": "^10.0.0",
"strip-ansi": "6.0.1",
"pkg-pr-new": "0.0.20",
"strip-ansi": "7.1.0",
"ts-morph": "16.0.0",
"typescript": "4.8.2",
"vitest": "^1.4.0"
"typescript": "5.5.4",
"vitest": "^2.0.0"
},

@@ -40,0 +40,0 @@ "scripts": {

+185
-32

@@ -5,6 +5,7 @@ # expect-type

![npm](https://img.shields.io/npm/dt/expect-type)
[![X (formerly Twitter) Follow](https://img.shields.io/twitter/follow/mmkal)](https://x.com/mmkalmmkal)
Compile-time tests for types. Useful to make sure types don't regress into being overly-permissive as changes go in over time.
Compile-time tests for types. Useful to make sure types don't regress into being overly permissive as changes go in over time.
Similar to Jest's `expect`, but with type-awareness. Gives you access to a number of type-matchers that let you make assertions about the form of a reference or generic type parameter.
Similar to `expect`, but with type-awareness. Gives you access to several type-matchers that let you make assertions about the form of a reference or generic type parameter.

@@ -23,3 +24,3 @@ ```ts

It can be used in your existing test files - or any other type-checked file you'd like - it's built into existing tooling with no dependencies. No extra build step, cli tool, IDE extension, or lint plugin is needed. Just import the function and start writing tests. Failures will be at compile time - they'll appear in your IDE and when you run `tsc`.
It can be used in your existing test files (and is actually [built in to vitest](https://vitest.dev/guide/testing-types)). Or it can be used in any other type-checked file you'd like - it's built into existing tooling with no dependencies. No extra build step, cli tool, IDE extension, or lint plugin is needed. Just import the function and start writing tests. Failures will be at compile time - they'll appear in your IDE and when you run `tsc`.

@@ -38,4 +39,6 @@ See below for lots more examples.

- [Error messages](#error-messages)
- [Concrete "expected" objects vs typeargs](#concrete-expected-objects-vs-typeargs)
- [Concrete "expected" objects vs type arguments](#concrete-expected-objects-vs-type-arguments)
- [Overloaded functions](#overloaded-functions)
- [Within test frameworks](#within-test-frameworks)
- [Vitest](#vitest)
- [Jest & `eslint-plugin-jest`](#jest--eslint-plugin-jest)

@@ -45,3 +48,5 @@ - [Limitations](#limitations)

- [Comparison](#comparison)
- [TypeScript backwards-compatibility](#typescript-backwards-compatibility)
- [Contributing](#contributing)
- [Documentation of limitations through tests](#documentation-of-limitations-through-tests)
<!-- codegen:end -->

@@ -61,3 +66,3 @@

The `expectTypeOf` method takes a single argument, or a generic parameter. Neither it, nor the functions chained off its return value, have any meaningful runtime behaviour. The assertions you write will be _compile-time_ errors if they don't hold true.
The `expectTypeOf` method takes a single argument or a generic type parameter. Neither it nor the functions chained off its return value have any meaningful runtime behaviour. The assertions you write will be _compile-time_ errors if they don't hold true.

@@ -69,4 +74,2 @@ ### Features

eslint prettier/prettier: ["warn", { "singleQuote": true, "semi": false, "arrowParens": "avoid", "trailingComma": "es5", "bracketSpacing": false, "endOfLine": "auto", "printWidth": 100 }]
```typescript

@@ -76,3 +79,3 @@ expectTypeOf({a: 1}).toEqualTypeOf<{a: number}>()

`.toEqualTypeOf` can check that two concrete objects have equivalent types (note: when these assertions _fail_, the error messages can be less informative vs the generic typearg syntax above - see [error messages docs](#error-messages)):
`.toEqualTypeOf` can check that two concrete objects have equivalent types (note: when these assertions _fail_, the error messages can be less informative vs the generic type argument syntax above - see [error messages docs](#error-messages)):

@@ -89,3 +92,3 @@ ```typescript

`.toEqualTypeOf` fails on extra properties:
`.toEqualTypeOf` fails on excess properties:

@@ -162,6 +165,4 @@ ```typescript

Test for basic javascript types:
You can test for basic JavaScript types:
eslint-disable-next-line vitest/valid-title
```typescript

@@ -179,3 +180,3 @@ expectTypeOf(() => 1).toBeFunction()

`.toBe...` methods allow for types which extend the expected type:
`.toBe...` methods allow for types that extend the expected type:

@@ -331,2 +332,31 @@ ```typescript

Up to ten overloads will produce union types for `.parameters` and `.returns`:
```typescript
type Factorize = {
(input: number): number[]
(input: bigint): bigint[]
}
expectTypeOf<Factorize>().parameters.toEqualTypeOf<[number] | [bigint]>()
expectTypeOf<Factorize>().returns.toEqualTypeOf<number[] | bigint[]>()
expectTypeOf<Factorize>().parameter(0).toEqualTypeOf<number | bigint>()
```
Note that these aren't exactly like TypeScript's built-in Parameters<...> and ReturnType<...>:
The TypeScript builtins simply choose a single overload (see the [Overloaded functions](#overloaded-functions) section for more information)
```typescript
type Factorize = {
(input: number): number[]
(input: bigint): bigint[]
}
// overload using `number` is ignored!
expectTypeOf<Parameters<Factorize>>().toEqualTypeOf<[bigint]>()
expectTypeOf<ReturnType<Factorize>>().toEqualTypeOf<bigint[]>()
```
More examples of ways to work with functions - parameters using `.parameter(n)` or `.parameters`, and return values using `.returns`:

@@ -353,2 +383,52 @@

`.toBeCallableWith` allows for overloads. You can also use it to narrow down the return type for given input parameters.:
```typescript
type Factorize = {
(input: number): number[]
(input: bigint): bigint[]
}
expectTypeOf<Factorize>().toBeCallableWith(6)
expectTypeOf<Factorize>().toBeCallableWith(6n)
```
`.toBeCallableWith` returns a type that can be used to narrow down the return type for given input parameters.:
```typescript
type Factorize = {
(input: number): number[]
(input: bigint): bigint[]
}
expectTypeOf<Factorize>().toBeCallableWith(6).returns.toEqualTypeOf<number[]>()
expectTypeOf<Factorize>().toBeCallableWith(6n).returns.toEqualTypeOf<bigint[]>()
```
`.toBeCallableWith` can be used to narrow down the parameters of a function:
```typescript
type Delete = {
(path: string): void
(paths: string[], options?: {force: boolean}): void
}
expectTypeOf<Delete>().toBeCallableWith('abc').parameters.toEqualTypeOf<[string]>()
expectTypeOf<Delete>()
.toBeCallableWith(['abc', 'def'], {force: true})
.parameters.toEqualTypeOf<[string[], {force: boolean}?]>()
expectTypeOf<Delete>().toBeCallableWith('abc').parameter(0).toBeString()
expectTypeOf<Delete>().toBeCallableWith('abc').parameter(1).toBeUndefined()
expectTypeOf<Delete>()
.toBeCallableWith(['abc', 'def', 'ghi'])
.parameter(0)
.toEqualTypeOf<string[]>()
expectTypeOf<Delete>()
.toBeCallableWith(['abc', 'def', 'ghi'])
.parameter(1)
.toEqualTypeOf<{force: boolean} | undefined>()
```
You can't use `.toBeCallableWith` with `.not` - you need to use ts-expect-error::

@@ -386,5 +466,35 @@

expectTypeOf(Date).constructorParameters.toEqualTypeOf<[] | [string | number | Date]>()
expectTypeOf(Date).constructorParameters.toEqualTypeOf<
| []
| [value: string | number]
| [value: string | number | Date]
| [
year: number,
monthIndex: number,
date?: number | undefined,
hours?: number | undefined,
minutes?: number | undefined,
seconds?: number | undefined,
ms?: number | undefined,
]
>()
```
Constructor overloads:
```typescript
class DBConnection {
constructor()
constructor(connectionString: string)
constructor(options: {host: string; port: number})
constructor(..._: unknown[]) {}
}
expectTypeOf(DBConnection).toBeConstructibleWith()
expectTypeOf(DBConnection).toBeConstructibleWith('localhost')
expectTypeOf(DBConnection).toBeConstructibleWith({host: 'localhost', port: 1234})
// @ts-expect-error - as when calling `new DBConnection(...)` you can't actually use the `(...args: unknown[])` overlaod, it's purely for the implementation.
expectTypeOf(DBConnection).toBeConstructibleWith(1, 2)
```
Check function `this` parameters:

@@ -467,3 +577,3 @@

Detect the difference between regular and readonly properties:
Detect the difference between regular and `readonly` properties:

@@ -528,3 +638,3 @@ ```typescript

But this won't work if the nesting is deeper in the type. For these situations, you can use the `.branded` helper. Note that this comes at a performance cost, and can cause the compiler to 'give up' if used with excessively deep types, so use sparingly. This helper is under `.branded` because it depply transforms the Actual and Expected types into a pseudo-AST:
But this won't work if the nesting is deeper in the type. For these situations, you can use the `.branded` helper. Note that this comes at a performance cost, and can cause the compiler to 'give up' if used with excessively deep types, so use sparingly. This helper is under `.branded` because it deeply transforms the Actual and Expected types into a pseudo-AST:

@@ -548,3 +658,3 @@ ```typescript

So, if you have an extremely deep type which ALSO has an intersection in it, you're out of luck and this library won't be able to test your type properly:
So, if you have an extremely deep type that ALSO has an intersection in it, you're out of luck and this library won't be able to test your type properly:

@@ -582,2 +692,17 @@ ```typescript

Overloads limitation for TypeScript <5.3: Due to a [TypeScript bug fixed in 5.3](https://github.com/microsoft/TypeScript/issues/28867), overloaded functions which include an overload resembling `(...args: unknown[]) => unknown` will exclude `unknown[]` from `.parameters` and exclude `unknown` from `.returns`:
```typescript
type Factorize = {
(...args: unknown[]): unknown
(input: number): number[]
(input: bigint): bigint[]
}
expectTypeOf<Factorize>().parameters.toEqualTypeOf<[number] | [bigint]>()
expectTypeOf<Factorize>().returns.toEqualTypeOf<number[] | bigint[]>()
```
This overload, however, allows any input and returns an unknown output anyway, so it's not very useful. If you are worried about this for some reason, you'll have to update TypeScript to 5.3+.
### Why is my assertion failing?

@@ -600,3 +725,3 @@

🚧 This library also exports some helper types for performing boolean operations on types, checking extension/equality in various ways, branding types, and checking for various special types like `never`, `any`, `unknown`. Use at your own risk! Nothing is stopping you using these beyond this warning:
🚧 This library also exports some helper types for performing boolean operations on types, checking extension/equality in various ways, branding types, and checking for various special types like `never`, `any`, `unknown`. Use at your own risk! Nothing is stopping you from using these beyond this warning:

@@ -609,3 +734,3 @@ >All internal types that are not documented here are _not_ part of the supported API surface, and may be renamed, modified, or removed, without warning or documentation in release notes.

When types don't match, `.toEqualTypeOf` and `.toMatchTypeOf` use a special helper type to produce error messages that are as actionable as possible. But there's a bit of an nuance to understanding them. Since the assertions are written "fluently", the failure should be on the "expected" type, not the "actual" type (`expect<Actual>().toEqualTypeOf<Expected>()`). This means that type errors can be a little confusing - so this library produces a `MismatchInfo` type to try to make explicit what the expectation is. For example:
When types don't match, `.toEqualTypeOf` and `.toMatchTypeOf` use a special helper type to produce error messages that are as actionable as possible. But there's a bit of a nuance to understanding them. Since the assertions are written "fluently", the failure should be on the "expected" type, not the "actual" type (`expect<Actual>().toEqualTypeOf<Expected>()`). This means that type errors can be a little confusing - so this library produces a `MismatchInfo` type to try to make explicit what the expectation is. For example:

@@ -626,5 +751,5 @@ ```ts

Note that the type constraint reported is a human-readable messaging specifying both the "expected" and "actual" types. Rather than taking the sentence `Types of property 'a' are incompatible // Type 'string' is not assignable to type "Expected: string, Actual: number"` literally - just look at the property name (`'a'`) and the message: `Expected: string, Actual: number`. This will tell you what's wrong, in most cases. Extremely complex types will of course be more effort to debug, and may require some experimentation. Please [raise an issue](https://github.com/mmkal/expect-type) if the error messages are actually misleading.
Note that the type constraint reported is a human-readable messaging specifying both the "expected" and "actual" types. Rather than taking the sentence `Types of property 'a' are incompatible // Type 'string' is not assignable to type "Expected: string, Actual: number"` literally - just look at the property name (`'a'`) and the message: `Expected: string, Actual: number`. This will tell you what's wrong, in most cases. Extremely complex types will, of course, be more effort to debug, and may require some experimentation. Please [raise an issue](https://github.com/mmkal/expect-type) if the error messages are misleading.
The `toBe...` methods (like `toBeString`, `toBeNumber`, `toBeVoid` etc.) fail by resolving to a non-callable type when the `Actual` type under test doesn't match up. For example, the failure for an assertion like `expectTypeOf(1).toBeString()` will look something like this:
The `toBe...` methods (like `toBeString`, `toBeNumber`, `toBeVoid`, etc.) fail by resolving to a non-callable type when the `Actual` type under test doesn't match up. For example, the failure for an assertion like `expectTypeOf(1).toBeString()` will look something like this:

@@ -641,5 +766,5 @@ ```

If TypeScript added support for ["throw" types](https://github.com/microsoft/TypeScript/pull/40468) these error messagess could be improved. Until then they will take a certain amount of squinting.
If TypeScript added support for ["throw" types](https://github.com/microsoft/TypeScript/pull/40468) these error messages could be improved. Until then they will take a certain amount of squinting.
#### Concrete "expected" objects vs typeargs
#### Concrete "expected" objects vs type arguments

@@ -658,3 +783,3 @@ Error messages for an assertion like this:

This is because the TypeScript compiler needs to infer the typearg for the `.toEqualTypeOf({a: ''})` style, and this library can only mark it as a failure by comparing it against a generic `Mismatch` type. So, where possible, use a typearg rather than a concrete type for `.toEqualTypeOf` and `toMatchTypeOf`. If it's much more convenient to compare two concrete types, you can use `typeof`:
This is because the TypeScript compiler needs to infer the type argument for the `.toEqualTypeOf({a: ''})` style and this library can only mark it as a failure by comparing it against a generic `Mismatch` type. So, where possible, use a type argument rather than a concrete type for `.toEqualTypeOf` and `toMatchTypeOf`. If it's much more convenient to compare two concrete types, you can use `typeof`:

@@ -668,4 +793,24 @@ ```ts

### Overloaded functions
Due to a TypeScript [design limitation](https://github.com/microsoft/TypeScript/issues/32164#issuecomment-506810756), the native TypeScript `Parameters<...>` and `ReturnType<...>` helpers only return types from one variant of an overloaded function. This limitation doesn't apply to expect-type, since it is not used to author TypeScript code, only to assert on existing types. So, we use a workaround for this TypeScript behaviour to assert on _all_ overloads as a union (actually, not necessarily _all_ - we cap out at 10 overloads).
### Within test frameworks
### Vitest
`expectTypeOf` is built in to [vitest](https://vitest.dev/guide/testing-types), so you can import `expectTypeOf` from the vitest library directly if you prefer. Note that there is no set release cadence, at time of writing, so vitest may not always be using the very latest version.
```ts
import {expectTypeOf} from 'vitest'
import {mount} from './mount.js'
test('my types work properly', () => {
expectTypeOf(mount).toBeFunction()
expectTypeOf(mount).parameter(0).toMatchTypeOf<{name: string}>()
expectTypeOf(mount({name: 42})).toBeString()
})
```
#### Jest & `eslint-plugin-jest`

@@ -675,3 +820,3 @@

To remove this warning, configure the ESlint rule to consider `expectTypeOf` as an assertion:
To remove this warning, configure the ESLint rule to consider `expectTypeOf` as an assertion:

@@ -695,7 +840,7 @@ ```json

A summary of some of the limitations of this library. Some of these is documented more fully elsewhere.
A summary of some of the limitations of this library. Some of these are documented more fully elsewhere.
1. Intersection types can result in failures when the expected and actual types are not identically defined, even when they are effectively identical. See [Why is my assertion failing](#why-is-my-assertion-failing) for details. TL;DR: use `.brand` in these cases - and accept the perf hit that it comes with.
1. `toBeCallableWith` will likely fail if you try to use it with a generic function or an overload. See [this issue](https://github.com/mmkal/expect-type/issues/50) for an example and how to work around.
1. (For now) overloaded functions might trip up the `.parameter` and `.parameters` helpers. This matches how the built-in typescript helper `Parameters<...>` works. This may be improved in the future though ([see related issue](https://github.com/mmkal/expect-type/issues/30)).
1. Intersection types can result in failures when the expected and actual types are not identically defined, even when they are effectively identical. See [Why is my assertion failing](#why-is-my-assertion-failing) for details. TL;DR: use `.brand` in these cases - and accept the performance hit that it comes with.
1. `toBeCallableWith` will likely fail if you try to use it with a generic function or an overload. See [this issue](https://github.com/mmkal/expect-type/issues/50) for an example and how to work around it.
1. (For now) overloaded functions might trip up the `.parameter` and `.parameters` helpers. This matches how the built-in TypeScript helper `Parameters<...>` works. This may be improved in the future though ([see related issue](https://github.com/mmkal/expect-type/issues/30)).
1. `expectTypeOf(this).toEqualTypeOf(this)` inside class methods does not work.

@@ -732,12 +877,20 @@

- built into existing tooling. No extra build step, cli tool, IDE extension, or lint plugin is needed. Just import the function and start writing tests. Failures will be at compile time - they'll appear in your IDE and when you run `tsc`.
- small implementation with no dependencies. [Take a look!](./src/index.ts) (tsd, for comparison, is [2.6MB](https://bundlephobia.com/result?p=tsd@0.13.1) because it ships a patched version of typescript).
- small implementation with no dependencies. [Take a look!](./src/index.ts) (tsd, for comparison, is [2.6MB](https://bundlephobia.com/result?p=tsd@0.13.1) because it ships a patched version of TypeScript).
## TypeScript backwards-compatibility
There is a CI job called `test-types` that checks whether the tests still pass with certain older TypeScript versions. To check the supported TypeScript versions, [refer to the job definition](./.github/workflows/ci.yml).
## Contributing
In most cases, it's worth checking existing issues or creating on to discuss a new feature or a bug fix before opening a pull request.
In most cases, it's worth checking existing issues or creating one to discuss a new feature or a bug fix before opening a pull request.
Once you're ready to make a pull request: clone the repo, and install pnpm if you don't have it already with `npm install --global pnpm`. Lockfiles for `npm` and `yarn` are gitignored.
If you're adding a feature, you should write a self-contained usage example in the form of a test, in [test/usage.test.ts](./test/usage.test.ts). This file is used to populate the bulk of this readme using [eslint-plugin-codegen](https://npmjs.com/package/eslint-plugin-codegen), and to generate an ["errors" test file](./test/errors.test.ts), which captures the error messages that are emitted for failing assertions by the typescript compiler. So, the test name should be written as a human-readable sentence explaining the usage example. Have a look at the existing tests for an idea of the style.
If you're adding a feature, you should write a self-contained usage example in the form of a test, in [test/usage.test.ts](./test/usage.test.ts). This file is used to populate the bulk of this readme using [eslint-plugin-codegen](https://npmjs.com/package/eslint-plugin-codegen), and to generate an ["errors" test file](./test/errors.test.ts), which captures the error messages that are emitted for failing assertions by the TypeScript compiler. So, the test name should be written as a human-readable sentence explaining the usage example. Have a look at the existing tests for an idea of the style.
After adding the tests, run `npm run lint -- --fix` to update the readme, and `npm test -- --updateSnapshot` to update the errors test. The generated documentation and tests should be pushed to the same branch as the source code, and submitted as a pull request. CI will test that the docs and tests are up to date if you forget to run these commands.
### Documentation of limitations through tests
Limitations of the library are documented through tests in `usage.test.ts`. This means that if a future TypeScript version (or library version) fixes the limitation, the test will start failing, and it will be automatically removed from the documentation once it no longer applies.