diff options
author | WJH <hou32hou@gmail.com> | 2020-07-03 00:03:15 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-02 18:03:15 +0200 |
commit | 538504f57c206cb3d10f9fd53bd0c3fc4bef4e6f (patch) | |
tree | 3bb0e6e1f4599b28aa528eecc7c4d2bb77b97341 /std/node/_util | |
parent | cc12e86fe36a77ecb0bb836ba82e823ca26abdc2 (diff) |
improve(std/asserts): allow assert functions to specify type parameter (#6413)
Diffstat (limited to 'std/node/_util')
-rw-r--r-- | std/node/_util/_util_callbackify_test.ts | 4 | ||||
-rw-r--r-- | std/node/_util/_util_promisify.ts | 25 | ||||
-rw-r--r-- | std/node/_util/_util_promisify_test.ts | 2 |
3 files changed, 24 insertions, 7 deletions
diff --git a/std/node/_util/_util_callbackify_test.ts b/std/node/_util/_util_callbackify_test.ts index d6a5d8664..e8a313905 100644 --- a/std/node/_util/_util_callbackify_test.ts +++ b/std/node/_util/_util_callbackify_test.ts @@ -246,7 +246,7 @@ Deno.test("callbackify passes arguments to the original", async () => { for (const value of values) { // eslint-disable-next-line require-await - async function asyncFn<T>(arg: T): Promise<T> { + async function asyncFn(arg: typeof value): Promise<typeof value> { assertStrictEquals(arg, value); return arg; } @@ -263,7 +263,7 @@ Deno.test("callbackify passes arguments to the original", async () => { }); }); - function promiseFn<T>(arg: T): Promise<T> { + function promiseFn<T>(arg: typeof value): Promise<typeof value> { assertStrictEquals(arg, value); return Promise.resolve(arg); } diff --git a/std/node/_util/_util_promisify.ts b/std/node/_util/_util_promisify.ts index e3cc36a0c..ea2fb6a5e 100644 --- a/std/node/_util/_util_promisify.ts +++ b/std/node/_util/_util_promisify.ts @@ -21,13 +21,30 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. +// Hack: work around the following TypeScript error: +// error: TS2345 [ERROR]: Argument of type 'typeof kCustomPromisifiedSymbol' +// is not assignable to parameter of type 'typeof kCustomPromisifiedSymbol'. +// assertStrictEquals(kCustomPromisifiedSymbol, promisify.custom); +// ~~~~~~~~~~~~~~~~ +declare const _CustomPromisifiedSymbol: unique symbol; +declare const _CustomPromisifyArgsSymbol: unique symbol; +declare let Symbol: SymbolConstructor; +interface SymbolConstructor { + for(key: "nodejs.util.promisify.custom"): typeof _CustomPromisifiedSymbol; + for( + key: "nodejs.util.promisify.customArgs" + ): typeof _CustomPromisifyArgsSymbol; +} +// End hack. + // In addition to being accessible through util.promisify.custom, -// this symbol is registered globally and can be accessed in any environment as Symbol.for('nodejs.util.promisify.custom') +// this symbol is registered globally and can be accessed in any environment as +// Symbol.for('nodejs.util.promisify.custom'). const kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom"); -// This is an internal Node symbol used by functions returning multiple arguments -// e.g. ['bytesRead', 'buffer'] for fs.read. +// This is an internal Node symbol used by functions returning multiple +// arguments, e.g. ['bytesRead', 'buffer'] for fs.read(). const kCustomPromisifyArgsSymbol = Symbol.for( - "deno.nodejs.util.promisify.customArgs" + "nodejs.util.promisify.customArgs" ); class NodeInvalidArgTypeError extends TypeError { diff --git a/std/node/_util/_util_promisify_test.ts b/std/node/_util/_util_promisify_test.ts index c6dbbd45a..4369a0132 100644 --- a/std/node/_util/_util_promisify_test.ts +++ b/std/node/_util/_util_promisify_test.ts @@ -30,7 +30,7 @@ import { promisify } from "./_util_promisify.ts"; import * as fs from "../fs.ts"; const readFile = promisify(fs.readFile); -const customPromisifyArgs = Symbol.for("deno.nodejs.util.promisify.customArgs"); +const customPromisifyArgs = Symbol.for("nodejs.util.promisify.customArgs"); Deno.test( "Errors should reject the promise", |