From 9ad5b0653e3821915d7ab7438d5ed4fe373c569a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=B7=E6=B8=A1?= Date: Tue, 18 Jun 2019 01:42:20 +0800 Subject: [PATCH] clearTimeout should convert to number (#2539) --- js/timers.ts | 1 + js/timers_test.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/js/timers.ts b/js/timers.ts index ba9a653a..0d0e1e69 100644 --- a/js/timers.ts +++ b/js/timers.ts @@ -250,6 +250,7 @@ export function setInterval( /** Clears a previously set timer by id. AKA clearTimeout and clearInterval. */ export function clearTimer(id: number): void { + id = Number(id); const timer = idMap.get(id); if (timer === undefined) { // Timer doesn't exist any more or never existed. This is not an error. diff --git a/js/timers_test.ts b/js/timers_test.ts index 88c5ea5c..65432d4c 100644 --- a/js/timers_test.ts +++ b/js/timers_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { test, assertEquals } from "./test_util.ts"; +import { test, assert, assertEquals } from "./test_util.ts"; function deferred(): { promise: Promise<{}>; @@ -231,3 +231,15 @@ test(async function timeoutBindThis(): Promise { } ); }); + +test(async function clearTimeoutShouldConvertToNumber(): Promise { + let called = false; + const obj = { + valueOf(): number { + called = true; + return 1; + } + }; + clearTimeout((obj as unknown) as number); + assert(called); +}); -- GitLab