feat(assert): Add support for TypeScript 3.7 assertion functions (#41927)

Co-Authored-By: Linus Unnebäck <linus@folkdatorn.se>

Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>
This commit is contained in:
ExE Boss 2020-02-04 01:06:27 +01:00 committed by GitHub
parent 7c12944ed1
commit 00a3593da3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 169 additions and 17 deletions

View File

@ -2,10 +2,14 @@ import * as assert from 'assert';
assert(true, "it's working");
assert.ok(true, "inner functions work as well");
assert.ok(true, 'inner functions work as well');
assert.throws(() => {});
assert.throws(() => {}, /Regex test/);
assert.throws(() => {}, () => {}, "works wonderfully");
assert.throws(
() => {},
() => {},
'works wonderfully',
);
assert['fail'](true, true, "works like a charm");
assert['fail'](true, true, 'works like a charm');

View File

@ -2,6 +2,7 @@
// Project: https://github.com/browserify/commonjs-assert, https://github.com/defunctzombie/commonjs-assert
// Definitions by: Nico Gallinal <https://github.com/nicoabie>
// Linus Unnebäck <https://github.com/LinusU>
// ExE Boss <https://github.com/ExE-Boss>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare function assert(value: any, message?: string): void;
@ -29,7 +30,11 @@ declare namespace assert {
function throws(block: () => void, error: (() => void) | ((err: any) => boolean) | RegExp, message?: string): void;
function doesNotThrow(block: () => void, message?: string): void;
function doesNotThrow(block: () => void, error: (() => void) | ((err: any) => boolean) | RegExp, message?: string): void;
function doesNotThrow(
block: () => void,
error: (() => void) | ((err: any) => boolean) | RegExp,
message?: string,
): void;
function ifError(value: any): void;
@ -41,7 +46,13 @@ declare namespace assert {
operator: string;
generatedMessage: boolean;
constructor(options?: { message?: string; actual?: any; expected?: any; operator?: string; stackStartFunction?: () => void });
constructor(options?: {
message?: string;
actual?: any;
expected?: any;
operator?: string;
stackStartFunction?: () => void;
});
}
}

View File

@ -0,0 +1,7 @@
{
"private": true,
"types": "index",
"typesVersions": {
">=3.7.0-0": { "*": ["ts3.7/*"] }
}
}

View File

@ -0,0 +1,59 @@
import * as assert from 'assert';
assert(true, "it's working");
assert.ok(true, 'inner functions work as well');
assert.throws(() => {});
assert.throws(() => {}, /Regex test/);
assert.throws(
() => {},
() => {},
'works wonderfully',
);
assert['fail'](true, true, 'works like a charm');
{
const a = null as any;
assert.ifError(a);
a; // $ExpectType null | undefined
}
{
const a = true as boolean;
assert(a);
a; // $ExpectType true
}
{
// tslint:disable-next-line: no-null-undefined-union
const a = 13 as number | null | undefined;
assert(a);
a; // $ExpectType number
}
{
const a = true as boolean;
assert.ok(a);
a; // $ExpectType true
}
{
// tslint:disable-next-line: no-null-undefined-union
const a = 13 as number | null | undefined;
assert.ok(a);
a; // $ExpectType number
}
{
const a = 'test' as any;
assert.strictEqual(a, 'test');
a; // $ExpectType string
}
{
const a = { b: 2 } as any;
assert.deepStrictEqual(a, { b: 2 });
a; // $ExpectType { b: number; }
}

58
types/assert/ts3.7/index.d.ts vendored Normal file
View File

@ -0,0 +1,58 @@
declare function assert(value: any, message?: string): asserts value;
declare namespace assert {
function fail(actual?: any, expected?: any, message?: string, operator?: string): void;
function ok(value: any, message?: string): asserts value;
/** @deprecated Use `strictEqual` instead */
function equal(actual: any, expected: any, message?: string): void;
/** @deprecated Use `notStrictEqual` instead */
function notEqual(actual: any, expected: any, message?: string): void;
/** @deprecated Use `deepStrictEqual` instead */
function deepEqual(actual: any, expected: any, message?: string): void;
/** @deprecated Use `notDeepStrictEqual` instead */
function notDeepEqual(actual: any, expected: any, message?: string): void;
function deepStrictEqual<T>(actual: any, expected: T, message?: string): asserts actual is T;
function notDeepStrictEqual(actual: any, expected: any, message?: string): void;
function strictEqual<T>(actual: any, expected: T, message?: string): asserts actual is T;
function notStrictEqual(actual: any, expected: any, message?: string): void;
function throws(block: () => void, message?: string): void;
function throws(block: () => void, error: (() => void) | ((err: any) => boolean) | RegExp, message?: string): void;
function doesNotThrow(block: () => void, message?: string): void;
function doesNotThrow(
block: () => void,
error: (() => void) | ((err: any) => boolean) | RegExp,
message?: string,
): void;
function ifError(value: any): asserts value is null | undefined;
class AssertionError implements Error {
name: string;
message: string;
actual: any;
expected: any;
operator: string;
generatedMessage: boolean;
constructor(options?: {
message?: string;
actual?: any;
expected?: any;
operator?: string;
stackStartFunction?: () => void;
});
}
}
export = assert;

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es2015"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../../",
"typeRoots": ["../../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"assert-tests.ts",
"index.d.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -1,24 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"lib": ["es2015"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"assert-tests.ts"
"assert-tests.ts",
"index.d.ts"
]
}

View File

@ -1,3 +1 @@
{
"extends": "dtslint/dt.json"
}
{ "extends": "dtslint/dt.json" }