Skip to content

Commit ab735ba

Browse files
committed
fix: fix handling of readonly tuple type
1 parent 79fef6a commit ab735ba

File tree

3 files changed

+141
-3
lines changed

3 files changed

+141
-3
lines changed

generated/lib.dom.d.ts

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34081,11 +34081,21 @@ 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.NeverOrUnknown<
34088+
BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
34089+
BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
34090+
>
34091+
>,
34092+
>(
3408634093
value: T,
3408734094
options?: StructuredSerializeOptions,
34088-
): T;
34095+
): BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<T>;
34096+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
34097+
// declare function structuredClone<T = any>(value: T, options?: StructuredSerializeOptions): T;
34098+
3408934099
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) */
3409034100
declare var sessionStorage: Storage;
3409134101
declare function addEventListener<K extends keyof WindowEventMap>(
@@ -34742,3 +34752,126 @@ type XMLHttpRequestResponseType =
3474234752
| "document"
3474334753
| "json"
3474434754
| "text";
34755+
// --------------------
34756+
34757+
declare namespace BetterTypeScriptLibInternals {
34758+
export namespace StructuredClone {
34759+
type Basics = [
34760+
EvalError,
34761+
RangeError,
34762+
ReferenceError,
34763+
TypeError,
34764+
SyntaxError,
34765+
URIError,
34766+
Error,
34767+
Boolean,
34768+
String,
34769+
Date,
34770+
RegExp,
34771+
];
34772+
type DOMSpecifics = [
34773+
DOMException,
34774+
DOMMatrix,
34775+
DOMMatrixReadOnly,
34776+
DOMPoint,
34777+
DOMPointReadOnly,
34778+
DOMQuad,
34779+
DOMRect,
34780+
DOMRectReadOnly,
34781+
];
34782+
type FileSystemTypeFamily = [
34783+
FileSystemDirectoryHandle,
34784+
FileSystemFileHandle,
34785+
FileSystemHandle,
34786+
];
34787+
type WebGPURelatedTypeFamily = [
34788+
// GPUCompilationInfo,
34789+
// GPUCompilationMessage,
34790+
];
34791+
type TypedArrayFamily = [
34792+
Int8Array,
34793+
Int16Array,
34794+
Int32Array,
34795+
BigInt64Array,
34796+
Uint8Array,
34797+
Uint16Array,
34798+
Uint32Array,
34799+
BigUint64Array,
34800+
Uint8ClampedArray,
34801+
];
34802+
type Weaken = [
34803+
...Basics,
34804+
// AudioData,
34805+
Blob,
34806+
// CropTarget,
34807+
// CryptoTarget,
34808+
...DOMSpecifics,
34809+
...FileSystemTypeFamily,
34810+
...WebGPURelatedTypeFamily,
34811+
File,
34812+
FileList,
34813+
...TypedArrayFamily,
34814+
DataView,
34815+
ImageBitmap,
34816+
ImageData,
34817+
RTCCertificate,
34818+
VideoFrame,
34819+
];
34820+
34821+
type MapSubtype<R> = {
34822+
[k in keyof Weaken]: R extends Weaken[k] ? true : false;
34823+
};
34824+
type SelectNumericLiteral<H> = number extends H ? never : H;
34825+
type FilterByNumericLiteralKey<R extends Record<string | number, any>> = {
34826+
[k in keyof R as `${R[k] extends true ? Exclude<SelectNumericLiteral<k>, symbol> : never}`]: [];
34827+
};
34828+
type HitWeakenEntry<E> = keyof FilterByNumericLiteralKey<MapSubtype<E>>;
34829+
34830+
type NonCloneablePrimitive =
34831+
| Function
34832+
| { new (...args: any[]): any }
34833+
| ((...args: any[]) => any)
34834+
| symbol;
34835+
34836+
type Writeable<T> = T extends readonly []
34837+
? []
34838+
: T extends readonly [infer X, ...infer XS]
34839+
? [X, ...XS]
34840+
: T extends { [x: PropertyKey]: any }
34841+
? {
34842+
-readonly [P in keyof T]: Writeable<T[P]>;
34843+
}
34844+
: T;
34845+
34846+
type StructuredCloneOutput<T> = T extends NonCloneablePrimitive
34847+
? never
34848+
: T extends ReadonlyArray<any>
34849+
? number extends T["length"]
34850+
? Array<StructuredCloneOutput<T[number]>>
34851+
: T extends readonly [infer X, ...infer XS]
34852+
? [StructuredCloneOutput<X>, ...StructuredCloneOutput<XS>]
34853+
: Writeable<T>
34854+
: T extends Map<infer K, infer V>
34855+
? Map<StructuredCloneOutput<K>, StructuredCloneOutput<V>>
34856+
: T extends Set<infer E>
34857+
? Set<StructuredCloneOutput<E>>
34858+
: T extends Record<any, any>
34859+
? HitWeakenEntry<T> extends never
34860+
? Writeable<{
34861+
-readonly [k in Exclude<
34862+
keyof T,
34863+
symbol
34864+
> as `${[StructuredCloneOutput<T[k]>] extends [never] ? never : k}`]: StructuredCloneOutput<
34865+
T[k]
34866+
>;
34867+
}>
34868+
: // hit
34869+
Weaken[HitWeakenEntry<T>]
34870+
: T;
34871+
34872+
type AvoidCyclicConstraint<T> = [T] extends [infer R] ? R : never;
34873+
34874+
// 上限が不正にきつくなっているのを無視する
34875+
type NeverOrUnknown<T> = [T] extends [never] ? never : unknown;
34876+
}
34877+
}

lib/lib.dom.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ declare namespace BetterTypeScriptLibInternals {
127127
: T extends ReadonlyArray<any>
128128
? number extends T["length"]
129129
? Array<StructuredCloneOutput<T[number]>>
130-
: T extends [infer X, ...infer XS]
130+
: T extends readonly [infer X, ...infer XS]
131131
? [StructuredCloneOutput<X>, ...StructuredCloneOutput<XS>]
132132
: Writeable<T>
133133
: T extends Map<infer K, infer V>

tests/src/dom.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ const test = async (url: string) => {
5757
// @ts-expect-error property does not exist
5858
structuredClone(f)[0].weirdo;
5959

60+
const f2: Weirdo[] = [new Weirdo()];
61+
expectType<Int16Array[]>(structuredClone(f2));
62+
// @ts-expect-error property does not exist
63+
structuredClone(f2)[0].weirdo;
64+
6065
const g = { a: new Weirdo() };
6166
const g2 = structuredClone(g);
6267
expectType<{ a: Int16Array }>(g2);

0 commit comments

Comments
 (0)