Skip to content

Commit

Permalink
feat: strongly typed typeof hints
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Feb 25, 2024
1 parent 540e83a commit bef7be6
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/chai/interface/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,25 @@ export interface AssertInterface {
isFinite(val: number, msg?: string): void;
isBoolean(val: unknown, msg?: string): asserts val is boolean;
isNotBoolean(val: unknown, msg?: string): void;

// typeof
typeOf(val: unknown, type: 'undefined'): asserts val is undefined;
typeOf(val: unknown, type: 'null'): asserts val is null;
typeOf(val: unknown, type: 'map'): asserts val is Map<unknown, unknown>;
typeOf(val: unknown, type: 'set'): asserts val is Set<unknown>;
typeOf(val: unknown, type: 'promise'): asserts val is Promise<unknown>;
typeOf(val: unknown, type: 'string'): asserts val is string;
typeOf(val: unknown, type: 'number'): asserts val is number;
typeOf(val: unknown, type: 'array'): asserts val is Array<unknown>;
typeOf(val: unknown, type: 'boolean'): asserts val is boolean;
typeOf(val: unknown, type: string, msg?: string): void;
notTypeOf(val: unknown, type: string, msg?: string): void;
instanceOf<T>(val: T, type: Constructor<T>, msg?: string): void;

// instanceof
instanceOf<T extends Constructor<unknown>>(val: unknown, type: T, msg?: string): asserts val is InstanceType<T>;
notInstanceOf(val: object, type: Constructor<unknown>, msg?: string): void;

// includes
include(expr: CollectionLike<unknown> | string | object, inc: unknown, msg?: string): void;
notInclude(expr: CollectionLike<unknown> | string | object, inc: unknown, msg?: string): void;
deepInclude(expr: CollectionLike<unknown> | string | object, inc: unknown, msg?: string): void;
Expand All @@ -90,8 +105,12 @@ export interface AssertInterface {
notOwnInclude(expr: object, inc: unknown, msg?: string): void;
deepOwnInclude(expr: object, inc: unknown, msg?: string): void;
notDeepOwnInclude(expr: object, inc: unknown, msg?: string): void;

// match
match(expr: string, re: RegExp, msg?: string): void;
notMatch(expr: string, re: RegExp, msg?: string): void;

// properties
property<T extends object, TKey extends PropertyKey>(
obj: T,
prop: TKey,
Expand Down Expand Up @@ -125,6 +144,7 @@ export interface AssertInterface {
notDeepNestedPropertyVal(obj: unknown, prop: string, val: unknown, msg?: string): void;
lengthOf(expr: LengthLike, len: number, msg?: string): void;

// keys
hasAnyKeys(
obj: Set<unknown> | Map<unknown, unknown>,
keys: unknown[],
Expand Down Expand Up @@ -280,6 +300,8 @@ export interface AssertInterface {
operator<T>(val: T, operator: string, val2: T, msg?: string): void;
closeTo(actual: number, expected: number, delta: number, msg?: string): void;
approximately(actual: number, expected: number, delta: number, msg?: string): void;

// members
sameMembers<T>(set1: T[], set2: T[], msg?: string): void;
notSameMembers<T>(set1: T[], set2: T[], msg?: string): void;
sameDeepMembers<T>(set1: T[], set2: T[], msg?: string): void;
Expand All @@ -296,6 +318,7 @@ export interface AssertInterface {
notIncludeOrderedMembers<T>(superset: T[], subset: T[], msg?: string): void;
includeDeepOrderedMembers<T>(superset: T[], subset: T[], msg?: string): void;
notIncludeDeepOrderedMembers<T>(superset: T[], subset: T[], msg?: string): void;

oneOf<T extends string | unknown[]>(inList: T, list: T[], msg?: string): void;

changes<T>(
Expand Down

0 comments on commit bef7be6

Please sign in to comment.