Skip to content
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
94 changes: 94 additions & 0 deletions types/chai/chai-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,8 @@ class CrashyObject {
}
}

declare function foobar<T>(): T;

suite("assert", () => {
test("assert", () => {
const foo = "bar" as string;
Expand Down Expand Up @@ -2490,3 +2492,95 @@ suite("assert", () => {
);
});
});

suite("narrowing", () => {
test("assert", () => {
const x = foobar<null | number>();
assert(typeof x === "number");
const y: number = x;
});

test("isOk", () => {
const x = foobar<null | number>();
assert.isOk(typeof x === "number");
const y: number = x;
});

test("ok", () => {
const x = foobar<null | number>();
assert.ok(typeof x === "number");
const y: number = x;
});

test("isTrue", () => {
const x = foobar<true | number>();
assert.isTrue(x);
const y: true = x;
});

test("isFalse", () => {
const x = foobar<false | number>();
assert.isFalse(x);
const y: false = x;
});

test("isNotTrue", () => {
const x = foobar<true | number>();
assert.isNotTrue(x);
const y: number = x;
});

test("isNotFalse", () => {
const x = foobar<false | number>();
assert.isNotFalse(x);
const y: number = x;
});

test("isNull", () => {
const x = foobar<null | number>();
assert.isNull(x);
const y: null = x;
});

test("isNotNull", () => {
const x = foobar<null | number>();
assert.isNotNull(x);
const y: number = x;
});

test("exists", () => {
const x = foobar<null | undefined | number>();
assert.exists(x);
const y: number = x;
});

test("notExists", () => {
const x = foobar<null | undefined | number>();
assert.notExists(x);
const y: null | undefined = x;
});

test("isUndefined", () => {
const x = foobar<undefined | number>();
assert.isUndefined(x);
const y: undefined = x;
});

test("isDefined", () => {
const x = foobar<undefined | number>();
assert.isDefined(x);
const y: number = x;
});

test("instanceOf", () => {
const x = foobar<Foo | null>();
assert.instanceOf(x, Foo);
const y: Foo = x;
});

test("notInstanceOf", () => {
const x = foobar<Foo | null>();
assert.notInstanceOf(x, Foo);
const y: null = x;
});
});
49 changes: 26 additions & 23 deletions types/chai/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ declare namespace Chai {
exists: boolean;
}

interface Constructor<T> {
new(...args: any[]): T;
}

interface ErrorConstructor {
new(...args: any[]): Error;
}
Expand Down Expand Up @@ -417,20 +421,18 @@ declare namespace Chai {
/**
* Asserts that object is truthy.
*
* T Type of object.
* @param object Object to test.
* @param message Message to display on error.
*/
isOk<T>(value: T, message?: string): void;
isOk(value: unknown, message?: string): asserts value;

/**
* Asserts that object is truthy.
*
* T Type of object.
* @param object Object to test.
* @param message Message to display on error.
*/
ok<T>(value: T, message?: string): void;
ok(value: unknown, message?: string): asserts value;

/**
* Asserts that object is falsy.
Expand Down Expand Up @@ -559,20 +561,18 @@ declare namespace Chai {
/**
* Asserts that value is true.
*
* T Type of value.
* @param value Actual value.
* @param message Message to display on error.
*/
isTrue<T>(value: T, message?: string): void;
isTrue(value: unknown, message?: string): asserts value is true;

/**
* Asserts that value is false.
*
* T Type of value.
* @param value Actual value.
* @param message Message to display on error.
*/
isFalse<T>(value: T, message?: string): void;
isFalse(value: unknown, message?: string): asserts value is false;

/**
* Asserts that value is not true.
Expand All @@ -581,25 +581,23 @@ declare namespace Chai {
* @param value Actual value.
* @param message Message to display on error.
*/
isNotTrue<T>(value: T, message?: string): void;
isNotTrue<T>(value: T, message?: string): asserts value is Exclude<T, true>;

/**
* Asserts that value is not false.
*
* T Type of value.
* @param value Actual value.
* @param message Message to display on error.
*/
isNotFalse<T>(value: T, message?: string): void;
isNotFalse<T>(value: T, message?: string): asserts value is Exclude<T, false>;

/**
* Asserts that value is null.
*
* T Type of value.
* @param value Actual value.
* @param message Message to display on error.
*/
isNull<T>(value: T, message?: string): void;
isNull(value: unknown, message?: string): asserts value is null;

/**
* Asserts that value is not null.
Expand All @@ -608,7 +606,7 @@ declare namespace Chai {
* @param value Actual value.
* @param message Message to display on error.
*/
isNotNull<T>(value: T, message?: string): void;
isNotNull<T>(value: T, message?: string): asserts value is Exclude<T, null>;

/**
* Asserts that value is NaN.
Expand All @@ -635,25 +633,25 @@ declare namespace Chai {
* @param value Actual value.
* @param message Message to display on error.
*/
exists<T>(value: T, message?: string): void;
exists<T>(value: T, message?: string): asserts value is NonNullable<T>;

/**
* Asserts that the target is either null or undefined.
*
* T Type of value.
* @param value Actual value.
* @param message Message to display on error.
*/
notExists<T>(value: T, message?: string): void;
notExists(value: unknown, message?: string): asserts value is
| null
| undefined;

/**
* Asserts that value is undefined.
*
* T Type of value.
* @param value Actual value.
* @param message Message to display on error.
*/
isUndefined<T>(value: T, message?: string): void;
isUndefined(value: unknown, message?: string): asserts value is undefined;

/**
* Asserts that value is not undefined.
Expand All @@ -662,7 +660,7 @@ declare namespace Chai {
* @param value Actual value.
* @param message Message to display on error.
*/
isDefined<T>(value: T, message?: string): void;
isDefined<T>(value: T, message?: string): asserts value is Exclude<T, undefined>;

/**
* Asserts that value is a function.
Expand Down Expand Up @@ -808,22 +806,27 @@ declare namespace Chai {
/**
* Asserts that value is an instance of constructor.
*
* T Type of value.
* T Expected type of value.
* @param value Actual value.
* @param constructor Potential expected contructor of value.
* @param message Message to display on error.
*/
instanceOf<T>(value: T, constructor: Function, message?: string): void;
instanceOf<T>(
value: unknown,
constructor: Constructor<T>,
message?: string,
): asserts value is T;

/**
* Asserts that value is not an instance of constructor.
*
* T Type of value.
* U Type that value shouldn't be an instance of.
* @param value Actual value.
* @param constructor Potential expected contructor of value.
* @param message Message to display on error.
*/
notInstanceOf<T>(value: T, type: Function, message?: string): void;
notInstanceOf<T, U>(value: T, type: Constructor<U>, message?: string): asserts value is Exclude<T, U>;

/**
* Asserts that haystack includes needle.
Expand Down