Skip to content

Commit 8e5f5db

Browse files
make structuredClone generic (#1520)
Co-authored-by: saschanaz <[email protected]>
1 parent e6154ef commit 8e5f5db

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

baselines/dom.generated.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17776,7 +17776,7 @@ interface WindowOrWorkerGlobalScope {
1777617776
reportError(e: any): void;
1777717777
setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
1777817778
setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
17779-
structuredClone(value: any, options?: StructuredSerializeOptions): any;
17779+
structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
1778017780
}
1778117781

1778217782
interface WindowSessionStorage {
@@ -19193,7 +19193,7 @@ declare function queueMicrotask(callback: VoidFunction): void;
1919319193
declare function reportError(e: any): void;
1919419194
declare function setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
1919519195
declare function setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
19196-
declare function structuredClone(value: any, options?: StructuredSerializeOptions): any;
19196+
declare function structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
1919719197
declare var sessionStorage: Storage;
1919819198
declare function addEventListener<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
1919919199
declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;

baselines/serviceworker.generated.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5751,7 +5751,7 @@ interface WindowOrWorkerGlobalScope {
57515751
reportError(e: any): void;
57525752
setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
57535753
setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
5754-
structuredClone(value: any, options?: StructuredSerializeOptions): any;
5754+
structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
57555755
}
57565756

57575757
interface WorkerGlobalScopeEventMap {
@@ -6129,7 +6129,7 @@ declare function queueMicrotask(callback: VoidFunction): void;
61296129
declare function reportError(e: any): void;
61306130
declare function setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
61316131
declare function setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
6132-
declare function structuredClone(value: any, options?: StructuredSerializeOptions): any;
6132+
declare function structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
61336133
declare function addEventListener<K extends keyof ServiceWorkerGlobalScopeEventMap>(type: K, listener: (this: ServiceWorkerGlobalScope, ev: ServiceWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
61346134
declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
61356135
declare function removeEventListener<K extends keyof ServiceWorkerGlobalScopeEventMap>(type: K, listener: (this: ServiceWorkerGlobalScope, ev: ServiceWorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void;

baselines/sharedworker.generated.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5602,7 +5602,7 @@ interface WindowOrWorkerGlobalScope {
56025602
reportError(e: any): void;
56035603
setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
56045604
setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
5605-
structuredClone(value: any, options?: StructuredSerializeOptions): any;
5605+
structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
56065606
}
56075607

56085608
interface WorkerEventMap extends AbstractWorkerEventMap {
@@ -6140,7 +6140,7 @@ declare function queueMicrotask(callback: VoidFunction): void;
61406140
declare function reportError(e: any): void;
61416141
declare function setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
61426142
declare function setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
6143-
declare function structuredClone(value: any, options?: StructuredSerializeOptions): any;
6143+
declare function structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
61446144
declare function addEventListener<K extends keyof SharedWorkerGlobalScopeEventMap>(type: K, listener: (this: SharedWorkerGlobalScope, ev: SharedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
61456145
declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
61466146
declare function removeEventListener<K extends keyof SharedWorkerGlobalScopeEventMap>(type: K, listener: (this: SharedWorkerGlobalScope, ev: SharedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void;

baselines/webworker.generated.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,7 +6092,7 @@ interface WindowOrWorkerGlobalScope {
60926092
reportError(e: any): void;
60936093
setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
60946094
setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
6095-
structuredClone(value: any, options?: StructuredSerializeOptions): any;
6095+
structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
60966096
}
60976097

60986098
interface WorkerEventMap extends AbstractWorkerEventMap {
@@ -6650,7 +6650,7 @@ declare function queueMicrotask(callback: VoidFunction): void;
66506650
declare function reportError(e: any): void;
66516651
declare function setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
66526652
declare function setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
6653-
declare function structuredClone(value: any, options?: StructuredSerializeOptions): any;
6653+
declare function structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
66546654
declare function cancelAnimationFrame(handle: number): void;
66556655
declare function requestAnimationFrame(callback: FrameRequestCallback): number;
66566656
declare function addEventListener<K extends keyof DedicatedWorkerGlobalScopeEventMap>(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;

inputfiles/overridingTypes.jsonc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,25 @@
252252
}
253253
}
254254
},
255+
"structuredClone": {
256+
"signature": {
257+
"0": {
258+
"typeParameters": [
259+
{
260+
"name": "T",
261+
"default": "any"
262+
}
263+
],
264+
"param": [
265+
{
266+
"name": "value",
267+
"overrideType": "T"
268+
}
269+
],
270+
"overrideType": "T"
271+
}
272+
}
273+
},
255274
"clearTimeout": {
256275
"signature": {
257276
"0": {

unittests/files/structuredClone.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function assertType<T>(_x: T) {}
2+
3+
const toBeCloned = {
4+
name: "abc",
5+
address: "test",
6+
age: 30,
7+
info: {
8+
url: "https://example.com",
9+
},
10+
} as const;
11+
12+
const nonMatchingType = { foo: "bar" } as const;
13+
const clone = structuredClone(toBeCloned);
14+
15+
assertType<typeof toBeCloned>(clone);
16+
// @ts-expect-error non matching type
17+
assertType<typeof nonMatchingType>(clone);

0 commit comments

Comments
 (0)