Skip to content

Commit 1bd8216

Browse files
authored
Merge pull request #38 from uhyo/fix-37
feat: improve structuredClone typing
2 parents bc8ab11 + ee94fbe commit 1bd8216

File tree

4 files changed

+543
-13
lines changed

4 files changed

+543
-13
lines changed

docs/diff/dom.generated.d.ts.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,148 @@ Index: dom.generated.d.ts
126126
}
127127

128128
declare var RTCStatsReport: {
129+
@@ -34051,13 +34071,20 @@
130+
handler: TimerHandler,
131+
timeout?: number,
132+
...arguments: any[]
133+
): number;
134+
+
135+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
136+
-declare function structuredClone<T = any>(
137+
+declare function structuredClone<
138+
+ const T extends BetterTypeScriptLibInternals.StructuredClone.NeverOrUnknown<
139+
+ BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
140+
+ BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
141+
+ >
142+
+ >,
143+
+>(
144+
value: T,
145+
options?: StructuredSerializeOptions,
146+
-): T;
147+
+): BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<T>;
148+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) */
149+
declare var sessionStorage: Storage;
150+
declare function addEventListener<K extends keyof WindowEventMap>(
151+
type: K,
152+
@@ -34712,4 +34739,119 @@
153+
| "blob"
154+
| "document"
155+
| "json"
156+
| "text";
157+
+// --------------------
158+
+
159+
+declare namespace BetterTypeScriptLibInternals {
160+
+ export namespace StructuredClone {
161+
+ type Basics = [
162+
+ EvalError,
163+
+ RangeError,
164+
+ ReferenceError,
165+
+ TypeError,
166+
+ SyntaxError,
167+
+ URIError,
168+
+ Error,
169+
+ Boolean,
170+
+ String,
171+
+ Date,
172+
+ RegExp,
173+
+ ];
174+
+ type DOMSpecifics = [
175+
+ DOMException,
176+
+ DOMMatrix,
177+
+ DOMMatrixReadOnly,
178+
+ DOMPoint,
179+
+ DOMPointReadOnly,
180+
+ DOMQuad,
181+
+ DOMRect,
182+
+ DOMRectReadOnly,
183+
+ ];
184+
+ type FileSystemTypeFamily = [
185+
+ FileSystemDirectoryHandle,
186+
+ FileSystemFileHandle,
187+
+ FileSystemHandle,
188+
+ ];
189+
+ type WebGPURelatedTypeFamily = [
190+
+ // GPUCompilationInfo,
191+
+ // GPUCompilationMessage,
192+
+ ];
193+
+ type TypedArrayFamily = [
194+
+ Int8Array,
195+
+ Int16Array,
196+
+ Int32Array,
197+
+ BigInt64Array,
198+
+ Uint8Array,
199+
+ Uint16Array,
200+
+ Uint32Array,
201+
+ BigUint64Array,
202+
+ Uint8ClampedArray,
203+
+ ];
204+
+ type Weaken = [
205+
+ ...Basics,
206+
+ // AudioData,
207+
+ Blob,
208+
+ // CropTarget,
209+
+ // CryptoTarget,
210+
+ ...DOMSpecifics,
211+
+ ...FileSystemTypeFamily,
212+
+ ...WebGPURelatedTypeFamily,
213+
+ File,
214+
+ FileList,
215+
+ ...TypedArrayFamily,
216+
+ DataView,
217+
+ ImageBitmap,
218+
+ ImageData,
219+
+ RTCCertificate,
220+
+ VideoFrame,
221+
+ ];
222+
+
223+
+ type MapSubtype<R> = {
224+
+ [k in keyof Weaken]: R extends Weaken[k] ? true : false;
225+
+ };
226+
+ type SelectNumericLiteral<H> = number extends H ? never : H;
227+
+ type FilterByNumericLiteralKey<R extends Record<string | number, any>> = {
228+
+ [k in keyof R as `${R[k] extends true ? Exclude<SelectNumericLiteral<k>, symbol> : never}`]: [];
229+
+ };
230+
+ type HitWeakenEntry<E> = keyof FilterByNumericLiteralKey<MapSubtype<E>>;
231+
+
232+
+ type NonCloneablePrimitive =
233+
+ | Function
234+
+ | { new (...args: any[]): any }
235+
+ | ((...args: any[]) => any)
236+
+ | symbol;
237+
+
238+
+ type StructuredCloneOutputObject<T> = {
239+
+ -readonly [K in Exclude<keyof T, symbol> as [
240+
+ StructuredCloneOutput<T[K]>,
241+
+ ] extends [never]
242+
+ ? never
243+
+ : K]: StructuredCloneOutput<T[K]>;
244+
+ };
245+
+
246+
+ type StructuredCloneOutput<T> = T extends NonCloneablePrimitive
247+
+ ? never
248+
+ : T extends ReadonlyArray<any>
249+
+ ? number extends T["length"]
250+
+ ? Array<StructuredCloneOutput<T[number]>>
251+
+ : T extends readonly [infer X, ...infer XS]
252+
+ ? [StructuredCloneOutput<X>, ...StructuredCloneOutput<XS>]
253+
+ : T extends []
254+
+ ? []
255+
+ : StructuredCloneOutputObject<T>
256+
+ : T extends Map<infer K, infer V>
257+
+ ? Map<StructuredCloneOutput<K>, StructuredCloneOutput<V>>
258+
+ : T extends Set<infer E>
259+
+ ? Set<StructuredCloneOutput<E>>
260+
+ : T extends Record<any, any>
261+
+ ? HitWeakenEntry<T> extends never
262+
+ ? StructuredCloneOutputObject<T>
263+
+ : Weaken[HitWeakenEntry<T>]
264+
+ : T;
265+
+
266+
+ type AvoidCyclicConstraint<T> = [T] extends [infer R] ? R : never;
267+
+
268+
+ // 上限が不正にきつくなっているのを無視する
269+
+ type NeverOrUnknown<T> = [T] extends [never] ? never : unknown;
270+
+ }
271+
+}
129272

130273
```

generated/lib.dom.d.ts

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34081,11 +34081,17 @@ declare function setTimeout(
3408134081
timeout?: number,
3408234082
...arguments: any[]
3408334083
): number;
34084+
3408434085
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
34085-
declare function structuredClone<T = any>(
34086+
declare function structuredClone<
34087+
const T extends BetterTypeScriptLibInternals.StructuredClone.Constraint<T>,
34088+
>(
3408634089
value: T,
3408734090
options?: StructuredSerializeOptions,
34088-
): T;
34091+
): BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<T>;
34092+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
34093+
// declare function structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
34094+
3408934095
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) */
3409034096
declare var sessionStorage: Storage;
3409134097
declare function addEventListener<K extends keyof WindowEventMap>(
@@ -34742,3 +34748,124 @@ type XMLHttpRequestResponseType =
3474234748
| "document"
3474334749
| "json"
3474434750
| "text";
34751+
// --------------------
34752+
34753+
declare namespace BetterTypeScriptLibInternals {
34754+
export namespace StructuredClone {
34755+
type Basics = [
34756+
EvalError,
34757+
RangeError,
34758+
ReferenceError,
34759+
TypeError,
34760+
SyntaxError,
34761+
URIError,
34762+
Error,
34763+
Boolean,
34764+
String,
34765+
Date,
34766+
RegExp,
34767+
];
34768+
type DOMSpecifics = [
34769+
DOMException,
34770+
DOMMatrix,
34771+
DOMMatrixReadOnly,
34772+
DOMPoint,
34773+
DOMPointReadOnly,
34774+
DOMQuad,
34775+
DOMRect,
34776+
DOMRectReadOnly,
34777+
];
34778+
type FileSystemTypeFamily = [
34779+
FileSystemDirectoryHandle,
34780+
FileSystemFileHandle,
34781+
FileSystemHandle,
34782+
];
34783+
type WebGPURelatedTypeFamily = [
34784+
// GPUCompilationInfo,
34785+
// GPUCompilationMessage,
34786+
];
34787+
type TypedArrayFamily = [
34788+
Int8Array,
34789+
Int16Array,
34790+
Int32Array,
34791+
BigInt64Array,
34792+
Uint8Array,
34793+
Uint16Array,
34794+
Uint32Array,
34795+
BigUint64Array,
34796+
Uint8ClampedArray,
34797+
];
34798+
type Weaken = [
34799+
...Basics,
34800+
// AudioData,
34801+
Blob,
34802+
// CropTarget,
34803+
// CryptoTarget,
34804+
...DOMSpecifics,
34805+
...FileSystemTypeFamily,
34806+
...WebGPURelatedTypeFamily,
34807+
File,
34808+
FileList,
34809+
...TypedArrayFamily,
34810+
DataView,
34811+
ImageBitmap,
34812+
ImageData,
34813+
RTCCertificate,
34814+
VideoFrame,
34815+
];
34816+
34817+
type MapSubtype<R> = {
34818+
[k in keyof Weaken]: R extends Weaken[k] ? true : false;
34819+
};
34820+
type SelectNumericLiteral<H> = number extends H ? never : H;
34821+
type FilterByNumericLiteralKey<R extends Record<string | number, any>> = {
34822+
[k in keyof R as `${R[k] extends true ? Exclude<SelectNumericLiteral<k>, symbol> : never}`]: [];
34823+
};
34824+
type HitWeakenEntry<E> = keyof FilterByNumericLiteralKey<MapSubtype<E>>;
34825+
34826+
type NonCloneablePrimitive =
34827+
| Function
34828+
| { new (...args: any[]): any }
34829+
| ((...args: any[]) => any)
34830+
| symbol;
34831+
34832+
type StructuredCloneOutputObject<T> = {
34833+
-readonly [K in Exclude<keyof T, symbol> as [
34834+
StructuredCloneOutput<T[K]>,
34835+
] extends [never]
34836+
? never
34837+
: K]: StructuredCloneOutput<T[K]>;
34838+
};
34839+
34840+
type StructuredCloneOutput<T> = T extends NonCloneablePrimitive
34841+
? never
34842+
: T extends ReadonlyArray<any>
34843+
? number extends T["length"]
34844+
? Array<StructuredCloneOutput<T[number]>>
34845+
: T extends readonly [infer X, ...infer XS]
34846+
? [StructuredCloneOutput<X>, ...StructuredCloneOutput<XS>]
34847+
: T extends []
34848+
? []
34849+
: StructuredCloneOutputObject<T>
34850+
: T extends Map<infer K, infer V>
34851+
? Map<StructuredCloneOutput<K>, StructuredCloneOutput<V>>
34852+
: T extends Set<infer E>
34853+
? Set<StructuredCloneOutput<E>>
34854+
: T extends Record<any, any>
34855+
? HitWeakenEntry<T> extends never
34856+
? StructuredCloneOutputObject<T>
34857+
: Weaken[HitWeakenEntry<T>]
34858+
: T;
34859+
34860+
type AvoidCyclicConstraint<T> = [T] extends [infer R] ? R : never;
34861+
34862+
type NeverOrUnknown<T> = [T] extends [never] ? never : unknown;
34863+
34864+
export type Constraint<T> =
34865+
BetterTypeScriptLibInternals.StructuredClone.NeverOrUnknown<
34866+
BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
34867+
BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
34868+
>
34869+
>;
34870+
}
34871+
}

0 commit comments

Comments
 (0)