Skip to content

Commit e910379

Browse files
authored
[@types/node] use assertion guards in assert module (#42786)
* feat(node): Add support for TypeScript 3.7 assertion functions * fix(adone): use `any` for type of `assert` module * chore(node): rename `internal` namespace to `assert` * chore(node): adjust `typesVersions` order * chore(node): clean up `typesVersions` ranges * Revert "chore(node): clean up `typesVersions` ranges" This reverts commit 913a1a6.
1 parent c198c62 commit e910379

51 files changed

Lines changed: 888 additions & 87 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

types/adone/glosses/std.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import assert = require("assert");
21
import fs = require("fs");
32
import path = require("path");
43
import util = require("util");
@@ -33,6 +32,8 @@ import timers = require("timers");
3332
import dgram = require("dgram");
3433
import perf_hooks = require("perf_hooks");
3534

35+
declare const assert: any;
36+
3637
export {
3738
assert,
3839
fs,

types/node/assert.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
declare module "assert" {
2-
function internal(value: any, message?: string | Error): void;
3-
namespace internal {
2+
function assert(value: any, message?: string | Error): void;
3+
namespace assert {
44
class AssertionError implements Error {
55
name: string;
66
message: string;
@@ -22,9 +22,13 @@ declare module "assert" {
2222
/** @deprecated since v10.0.0 - use fail([message]) or other assert functions instead. */
2323
function fail(actual: any, expected: any, message?: string | Error, operator?: string, stackStartFn?: Function): never;
2424
function ok(value: any, message?: string | Error): void;
25+
/** @deprecated since v9.9.0 - use strictEqual() instead. */
2526
function equal(actual: any, expected: any, message?: string | Error): void;
27+
/** @deprecated since v9.9.0 - use notStrictEqual() instead. */
2628
function notEqual(actual: any, expected: any, message?: string | Error): void;
29+
/** @deprecated since v9.9.0 - use deepStrictEqual() instead. */
2730
function deepEqual(actual: any, expected: any, message?: string | Error): void;
31+
/** @deprecated since v9.9.0 - use notDeepStrictEqual() instead. */
2832
function notDeepEqual(actual: any, expected: any, message?: string | Error): void;
2933
function strictEqual(actual: any, expected: any, message?: string | Error): void;
3034
function notStrictEqual(actual: any, expected: any, message?: string | Error): void;
@@ -46,8 +50,8 @@ declare module "assert" {
4650
function match(value: string, regExp: RegExp, message?: string | Error): void;
4751
function doesNotMatch(value: string, regExp: RegExp, message?: string | Error): void;
4852

49-
const strict: typeof internal;
53+
const strict: typeof assert;
5054
}
5155

52-
export = internal;
56+
export = assert;
5357
}

types/node/base.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// base definnitions for all NodeJS modules that are not specific to any version of TypeScript
22
/// <reference path="globals.d.ts" />
3-
/// <reference path="assert.d.ts" />
43
/// <reference path="async_hooks.d.ts" />
54
/// <reference path="buffer.d.ts" />
65
/// <reference path="child_process.d.ts" />

types/node/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
// Base definitions for all NodeJS modules that are not specific to any version of TypeScript:
6060
/// <reference path="base.d.ts" />
6161

62+
// We can't include assert.d.ts in base.d.ts, as it'll cause duplication errors in +ts3.7
63+
/// <reference path="assert.d.ts" />
64+
6265
// Forward-declarations for needed types from es2015 and later (in case users are using `--lib es5`)
6366
// Empty interfaces are used here which merge fine with the real declarations in the lib XXX files
6467
// just to ensure the names are known and node typings can be used without importing these libs.

types/node/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
"private": true,
33
"types": "index",
44
"typesVersions": {
5+
">=3.7.0-0": {
6+
"*": [
7+
"ts3.7/*"
8+
]
9+
},
510
">=3.5.0-0": {
611
"*": [
712
"ts3.5/*"

types/node/ts3.2/base.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// NOTE: These definitions support NodeJS and TypeScript 3.2.
2+
3+
// NOTE: TypeScript version-specific augmentations can be found in the following paths:
4+
// - ~/base.d.ts - Shared definitions common to all TypeScript versions
5+
// - ~/index.d.ts - Definitions specific to TypeScript 2.1
6+
// - ~/ts3.2/base.d.ts - Definitions specific to TypeScript 3.2
7+
// - ~/ts3.2/index.d.ts - Definitions specific to TypeScript 3.2 with assert pulled in
8+
9+
// Reference required types from the default lib:
10+
/// <reference lib="es2018" />
11+
/// <reference lib="esnext.asynciterable" />
12+
/// <reference lib="esnext.intl" />
13+
/// <reference lib="esnext.bigint" />
14+
15+
// Base definitions for all NodeJS modules that are not specific to any version of TypeScript:
16+
// tslint:disable-next-line:no-bad-reference
17+
/// <reference path="../base.d.ts" />
18+
19+
// TypeScript 3.2-specific augmentations:
20+
/// <reference path="fs.d.ts" />
21+
/// <reference path="util.d.ts" />
22+
/// <reference path="globals.d.ts" />

types/node/ts3.2/index.d.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
// NOTE: These definitions support NodeJS and TypeScript 3.2.
2+
// This is requried to enable typing assert in ts3.7 without causing errors
3+
// Typically type modifiations should be made in base.d.ts instead of here
24

3-
// Reference required types from the default lib:
4-
/// <reference lib="es2018" />
5-
/// <reference lib="esnext.asynciterable" />
6-
/// <reference lib="esnext.intl" />
7-
/// <reference lib="esnext.bigint" />
5+
/// <reference path="base.d.ts" />
86

9-
// Base definitions for all NodeJS modules that are not specific to any version of TypeScript:
107
// tslint:disable-next-line:no-bad-reference
11-
/// <reference path="../base.d.ts" />
12-
13-
// TypeScript 3.2-specific augmentations:
14-
/// <reference path="fs.d.ts" />
15-
/// <reference path="util.d.ts" />
16-
/// <reference path="globals.d.ts" />
8+
/// <reference path="../assert.d.ts" />

types/node/ts3.5/base.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// NOTE: These definitions support NodeJS and TypeScript 3.5.
2+
3+
// NOTE: TypeScript version-specific augmentations can be found in the following paths:
4+
// - ~/base.d.ts - Shared definitions common to all TypeScript versions
5+
// - ~/index.d.ts - Definitions specific to TypeScript 2.1
6+
// - ~/ts3.5/base.d.ts - Definitions specific to TypeScript 3.5
7+
// - ~/ts3.5/index.d.ts - Definitions specific to TypeScript 3.5 with assert pulled in
8+
9+
// Reference required types from the default lib:
10+
/// <reference lib="es2018" />
11+
/// <reference lib="esnext.asynciterable" />
12+
/// <reference lib="esnext.intl" />
13+
/// <reference lib="esnext.bigint" />
14+
15+
// Base definitions for all NodeJS modules that are not specific to any version of TypeScript:
16+
// tslint:disable-next-line:no-bad-reference
17+
/// <reference path="../ts3.2/base.d.ts" />
18+
19+
// TypeScript 3.5-specific augmentations:
20+
/// <reference path="wasi.d.ts" />

types/node/ts3.5/index.d.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
// NOTE: These definitions support NodeJS and TypeScript 3.5.
2+
// This is requried to enable typing assert in ts3.7 without causing errors
3+
// Typically type modifiations should be made in base.d.ts instead of here
24

3-
// Reference required types from the default lib:
4-
/// <reference lib="es2018" />
5-
/// <reference lib="esnext.asynciterable" />
6-
/// <reference lib="esnext.intl" />
7-
/// <reference lib="esnext.bigint" />
5+
/// <reference path="base.d.ts" />
86

9-
// Base definitions for all NodeJS modules that are not specific to any version of TypeScript:
107
// tslint:disable-next-line:no-bad-reference
11-
/// <reference path="../ts3.2/index.d.ts" />
12-
13-
// TypeScript 3.5-specific augmentations:
14-
/// <reference path="wasi.d.ts" />
8+
/// <reference path="../assert.d.ts" />

types/node/ts3.7/assert.d.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
declare module "assert" {
2+
function assert(value: any, message?: string | Error): asserts value;
3+
namespace assert {
4+
class AssertionError implements Error {
5+
name: string;
6+
message: string;
7+
actual: any;
8+
expected: any;
9+
operator: string;
10+
generatedMessage: boolean;
11+
code: 'ERR_ASSERTION';
12+
13+
constructor(options?: {
14+
message?: string; actual?: any; expected?: any;
15+
operator?: string; stackStartFn?: Function
16+
});
17+
}
18+
19+
type AssertPredicate = RegExp | (new() => object) | ((thrown: any) => boolean) | object | Error;
20+
21+
function fail(message?: string | Error): never;
22+
/** @deprecated since v10.0.0 - use fail([message]) or other assert functions instead. */
23+
function fail(actual: any, expected: any, message?: string | Error, operator?: string, stackStartFn?: Function): never;
24+
function ok(value: any, message?: string | Error): asserts value;
25+
/** @deprecated since v9.9.0 - use strictEqual() instead. */
26+
function equal(actual: any, expected: any, message?: string | Error): void;
27+
/** @deprecated since v9.9.0 - use notStrictEqual() instead. */
28+
function notEqual(actual: any, expected: any, message?: string | Error): void;
29+
/** @deprecated since v9.9.0 - use deepStrictEqual() instead. */
30+
function deepEqual(actual: any, expected: any, message?: string | Error): void;
31+
/** @deprecated since v9.9.0 - use notDeepStrictEqual() instead. */
32+
function notDeepEqual(actual: any, expected: any, message?: string | Error): void;
33+
function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
34+
function notStrictEqual(actual: any, expected: any, message?: string | Error): void;
35+
function deepStrictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
36+
function notDeepStrictEqual(actual: any, expected: any, message?: string | Error): void;
37+
38+
function throws(block: () => any, message?: string | Error): void;
39+
function throws(block: () => any, error: AssertPredicate, message?: string | Error): void;
40+
function doesNotThrow(block: () => any, message?: string | Error): void;
41+
function doesNotThrow(block: () => any, error: RegExp | Function, message?: string | Error): void;
42+
43+
function ifError(value: any): asserts value is null | undefined;
44+
45+
function rejects(block: (() => Promise<any>) | Promise<any>, message?: string | Error): Promise<void>;
46+
function rejects(block: (() => Promise<any>) | Promise<any>, error: AssertPredicate, message?: string | Error): Promise<void>;
47+
function doesNotReject(block: (() => Promise<any>) | Promise<any>, message?: string | Error): Promise<void>;
48+
function doesNotReject(block: (() => Promise<any>) | Promise<any>, error: RegExp | Function, message?: string | Error): Promise<void>;
49+
50+
function match(value: string, regExp: RegExp, message?: string | Error): void;
51+
function doesNotMatch(value: string, regExp: RegExp, message?: string | Error): void;
52+
53+
const strict: typeof assert;
54+
}
55+
56+
export = assert;
57+
}

0 commit comments

Comments
 (0)