Jungzl 5 days ago committed by GitHub
commit c8dcc63bef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: enhance snapshot type handling for array structures in $state.snapshot

@ -86,7 +86,13 @@ declare namespace $state {
: T extends { toJSON(): infer R }
? R
: T extends readonly unknown[]
? { [K in keyof T]: Snapshot<T[K]> }
? number extends T['length']
? T extends Array<infer U>
? Array<Snapshot<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<Snapshot<U>>
: never
: { [K in keyof T]: Snapshot<T[K]> }
: T extends Array<infer U>
? Array<Snapshot<U>>
: T extends object

@ -0,0 +1,15 @@
import type { ClassValue } from 'clsx';
// Regression test for #17117: this should not trigger deep/infinite type instantiation
const cls = $state.raw<ClassValue[]>([]);
const cls_snap: ReturnType<typeof $state.snapshot<ClassValue[]>> =
$state.snapshot<ClassValue[]>(cls);
type SnapshotTuple = ReturnType<typeof $state.snapshot<[number, string]>>;
const tuple_ok: SnapshotTuple = [1, 'a'];
tuple_ok[0] === 1;
tuple_ok[1] === 'a';
// @ts-expect-error tuple element type should be preserved
const tuple_wrong: SnapshotTuple = [1, 2];

@ -3284,7 +3284,13 @@ declare namespace $state {
: T extends { toJSON(): infer R }
? R
: T extends readonly unknown[]
? { [K in keyof T]: Snapshot<T[K]> }
? number extends T['length']
? T extends Array<infer U>
? Array<Snapshot<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<Snapshot<U>>
: never
: { [K in keyof T]: Snapshot<T[K]> }
: T extends Array<infer U>
? Array<Snapshot<U>>
: T extends object

Loading…
Cancel
Save