Skip to content

Commit 4668dd1

Browse files
authored
Engsys/prepare for types node v16 (#25609)
- with `globalThis`, old logic to get global object is no longer needed. - After upgrading TypeScript compiler couldn't resolve to proper `setTimeout` overload because we use `lib: []` compiler option. Working around by casting callback to `(args: any) => void` or just returning `Promise<void>`
1 parent 56a5bd5 commit 4668dd1

File tree

3 files changed

+5
-19
lines changed

3 files changed

+5
-19
lines changed

sdk/anomalydetector/ai-anomaly-detector-rest/samples-dev/sample_multivariate_detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const apiKey = process.env["ANOMALY_DETECTOR_API_KEY"] || "";
2525
const endpoint = process.env["ANOMALY_DETECTOR_ENDPOINT"] || "";
2626
const dataSource = "<your data source>";
2727

28-
function sleep(time: number): Promise<NodeJS.Timer> {
28+
function sleep(time: number): Promise<void> {
2929
return new Promise((resolve) => setTimeout(resolve, time));
3030
}
3131

sdk/core/core-amqp/src/util/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,12 @@ export const defaultCancellableLock: CancellableAsyncLock = new CancellableAsync
122122
* the promise with the given value.
123123
*/
124124
export class Timeout {
125-
// Node and browsers return different types from setTimeout
126-
// Any is the easiest way to avoid type errors in either platform
127-
private _timer?: any;
125+
private _timer?: ReturnType<typeof setTimeout>;
128126

129127
set<T>(t: number, value?: T): Promise<T> {
130128
return new Promise<T>((resolve, reject) => {
131129
this.clear();
132-
const callback = value ? () => reject(new Error(`${value}`)) : resolve;
130+
const callback: (args: any) => void = value ? () => reject(new Error(`${value}`)) : resolve;
133131
this._timer = setTimeout(callback, t);
134132
});
135133
}

sdk/core/core-amqp/test/requestResponse.spec.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ import { assert } from "chai";
2323
import { createConnectionStub } from "./utils/createConnectionStub";
2424
import { isError } from "@azure/core-util";
2525

26-
interface Window {}
27-
declare let self: Window & typeof globalThis;
28-
29-
function getGlobal(): NodeJS.Global | (Window & typeof globalThis) {
30-
if (typeof global !== "undefined") {
31-
return global;
32-
} else {
33-
return self;
34-
}
35-
}
36-
3726
const assertItemsLengthInResponsesMap = (
3827
_responsesMap: Map<string, DeferredPromiseWithCallback>,
3928
expectedNumberOfItems: number
@@ -622,20 +611,19 @@ describe("RequestResponseLink", function () {
622611
});
623612

624613
describe("sendRequest clears timeout", () => {
625-
const _global = getGlobal();
626614
const originalClearTimeout = clearTimeout;
627615
let clearTimeoutCalledCount = 0;
628616

629617
beforeEach(() => {
630618
clearTimeoutCalledCount = 0;
631-
_global.clearTimeout = (tid: any) => {
619+
globalThis.clearTimeout = (tid: any) => {
632620
clearTimeoutCalledCount++;
633621
return originalClearTimeout(tid);
634622
};
635623
});
636624

637625
afterEach(() => {
638-
_global.clearTimeout = originalClearTimeout;
626+
globalThis.clearTimeout = originalClearTimeout;
639627
});
640628

641629
it("sendRequest clears timeout after error message", async function () {

0 commit comments

Comments
 (0)