Skip to content

feat: improve structuredClone typing #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions docs/diff/dom.generated.d.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,148 @@ Index: dom.generated.d.ts
}

declare var RTCStatsReport: {
@@ -34051,13 +34071,20 @@
handler: TimerHandler,
timeout?: number,
...arguments: any[]
): number;
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
-declare function structuredClone<T = any>(
+declare function structuredClone<
+ const T extends BetterTypeScriptLibInternals.StructuredClone.NeverOrUnknown<
+ BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
+ BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
+ >
+ >,
+>(
value: T,
options?: StructuredSerializeOptions,
-): T;
+): BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<T>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) */
declare var sessionStorage: Storage;
declare function addEventListener<K extends keyof WindowEventMap>(
type: K,
@@ -34712,4 +34739,119 @@
| "blob"
| "document"
| "json"
| "text";
+// --------------------
+
+declare namespace BetterTypeScriptLibInternals {
+ export namespace StructuredClone {
+ type Basics = [
+ EvalError,
+ RangeError,
+ ReferenceError,
+ TypeError,
+ SyntaxError,
+ URIError,
+ Error,
+ Boolean,
+ String,
+ Date,
+ RegExp,
+ ];
+ type DOMSpecifics = [
+ DOMException,
+ DOMMatrix,
+ DOMMatrixReadOnly,
+ DOMPoint,
+ DOMPointReadOnly,
+ DOMQuad,
+ DOMRect,
+ DOMRectReadOnly,
+ ];
+ type FileSystemTypeFamily = [
+ FileSystemDirectoryHandle,
+ FileSystemFileHandle,
+ FileSystemHandle,
+ ];
+ type WebGPURelatedTypeFamily = [
+ // GPUCompilationInfo,
+ // GPUCompilationMessage,
+ ];
+ type TypedArrayFamily = [
+ Int8Array,
+ Int16Array,
+ Int32Array,
+ BigInt64Array,
+ Uint8Array,
+ Uint16Array,
+ Uint32Array,
+ BigUint64Array,
+ Uint8ClampedArray,
+ ];
+ type Weaken = [
+ ...Basics,
+ // AudioData,
+ Blob,
+ // CropTarget,
+ // CryptoTarget,
+ ...DOMSpecifics,
+ ...FileSystemTypeFamily,
+ ...WebGPURelatedTypeFamily,
+ File,
+ FileList,
+ ...TypedArrayFamily,
+ DataView,
+ ImageBitmap,
+ ImageData,
+ RTCCertificate,
+ VideoFrame,
+ ];
+
+ type MapSubtype<R> = {
+ [k in keyof Weaken]: R extends Weaken[k] ? true : false;
+ };
+ type SelectNumericLiteral<H> = number extends H ? never : H;
+ type FilterByNumericLiteralKey<R extends Record<string | number, any>> = {
+ [k in keyof R as `${R[k] extends true ? Exclude<SelectNumericLiteral<k>, symbol> : never}`]: [];
+ };
+ type HitWeakenEntry<E> = keyof FilterByNumericLiteralKey<MapSubtype<E>>;
+
+ type NonCloneablePrimitive =
+ | Function
+ | { new (...args: any[]): any }
+ | ((...args: any[]) => any)
+ | symbol;
+
+ type StructuredCloneOutputObject<T> = {
+ -readonly [K in Exclude<keyof T, symbol> as [
+ StructuredCloneOutput<T[K]>,
+ ] extends [never]
+ ? never
+ : K]: StructuredCloneOutput<T[K]>;
+ };
+
+ type StructuredCloneOutput<T> = T extends NonCloneablePrimitive
+ ? never
+ : T extends ReadonlyArray<any>
+ ? number extends T["length"]
+ ? Array<StructuredCloneOutput<T[number]>>
+ : T extends readonly [infer X, ...infer XS]
+ ? [StructuredCloneOutput<X>, ...StructuredCloneOutput<XS>]
+ : T extends []
+ ? []
+ : StructuredCloneOutputObject<T>
+ : T extends Map<infer K, infer V>
+ ? Map<StructuredCloneOutput<K>, StructuredCloneOutput<V>>
+ : T extends Set<infer E>
+ ? Set<StructuredCloneOutput<E>>
+ : T extends Record<any, any>
+ ? HitWeakenEntry<T> extends never
+ ? StructuredCloneOutputObject<T>
+ : Weaken[HitWeakenEntry<T>]
+ : T;
+
+ type AvoidCyclicConstraint<T> = [T] extends [infer R] ? R : never;
+
+ // 上限が不正にきつくなっているのを無視する
+ type NeverOrUnknown<T> = [T] extends [never] ? never : unknown;
+ }
+}

```
131 changes: 129 additions & 2 deletions generated/lib.dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34081,11 +34081,17 @@ declare function setTimeout(
timeout?: number,
...arguments: any[]
): number;

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
declare function structuredClone<T = any>(
declare function structuredClone<
const T extends BetterTypeScriptLibInternals.StructuredClone.Constraint<T>,
>(
value: T,
options?: StructuredSerializeOptions,
): T;
): BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<T>;
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
// declare function structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) */
declare var sessionStorage: Storage;
declare function addEventListener<K extends keyof WindowEventMap>(
Expand Down Expand Up @@ -34742,3 +34748,124 @@ type XMLHttpRequestResponseType =
| "document"
| "json"
| "text";
// --------------------

declare namespace BetterTypeScriptLibInternals {
export namespace StructuredClone {
type Basics = [
EvalError,
RangeError,
ReferenceError,
TypeError,
SyntaxError,
URIError,
Error,
Boolean,
String,
Date,
RegExp,
];
type DOMSpecifics = [
DOMException,
DOMMatrix,
DOMMatrixReadOnly,
DOMPoint,
DOMPointReadOnly,
DOMQuad,
DOMRect,
DOMRectReadOnly,
];
type FileSystemTypeFamily = [
FileSystemDirectoryHandle,
FileSystemFileHandle,
FileSystemHandle,
];
type WebGPURelatedTypeFamily = [
// GPUCompilationInfo,
// GPUCompilationMessage,
];
type TypedArrayFamily = [
Int8Array,
Int16Array,
Int32Array,
BigInt64Array,
Uint8Array,
Uint16Array,
Uint32Array,
BigUint64Array,
Uint8ClampedArray,
];
type Weaken = [
...Basics,
// AudioData,
Blob,
// CropTarget,
// CryptoTarget,
...DOMSpecifics,
...FileSystemTypeFamily,
...WebGPURelatedTypeFamily,
File,
FileList,
...TypedArrayFamily,
DataView,
ImageBitmap,
ImageData,
RTCCertificate,
VideoFrame,
];

type MapSubtype<R> = {
[k in keyof Weaken]: R extends Weaken[k] ? true : false;
};
type SelectNumericLiteral<H> = number extends H ? never : H;
type FilterByNumericLiteralKey<R extends Record<string | number, any>> = {
[k in keyof R as `${R[k] extends true ? Exclude<SelectNumericLiteral<k>, symbol> : never}`]: [];
};
type HitWeakenEntry<E> = keyof FilterByNumericLiteralKey<MapSubtype<E>>;

type NonCloneablePrimitive =
| Function
| { new (...args: any[]): any }
| ((...args: any[]) => any)
| symbol;

type StructuredCloneOutputObject<T> = {
-readonly [K in Exclude<keyof T, symbol> as [
StructuredCloneOutput<T[K]>,
] extends [never]
? never
: K]: StructuredCloneOutput<T[K]>;
};

type StructuredCloneOutput<T> = T extends NonCloneablePrimitive
? never
: T extends ReadonlyArray<any>
? number extends T["length"]
? Array<StructuredCloneOutput<T[number]>>
: T extends readonly [infer X, ...infer XS]
? [StructuredCloneOutput<X>, ...StructuredCloneOutput<XS>]
: T extends []
? []
: StructuredCloneOutputObject<T>
: T extends Map<infer K, infer V>
? Map<StructuredCloneOutput<K>, StructuredCloneOutput<V>>
: T extends Set<infer E>
? Set<StructuredCloneOutput<E>>
: T extends Record<any, any>
? HitWeakenEntry<T> extends never
? StructuredCloneOutputObject<T>
: Weaken[HitWeakenEntry<T>]
: T;

type AvoidCyclicConstraint<T> = [T] extends [infer R] ? R : never;

type NeverOrUnknown<T> = [T] extends [never] ? never : unknown;

export type Constraint<T> =
BetterTypeScriptLibInternals.StructuredClone.NeverOrUnknown<
BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
>
>;
}
}
Loading
Loading