Skip to content

$state.snapshot with BigInt properties infer as never #18385

Description

@NickantX

Describe the bug

Hey, I noticed when I try using $state.snapshot with an object that contains a BigInt, the property is inferred as never.

In the types for $state.snapshot:

	type Snapshot<T> = T extends Primitive
		? T
		: T extends Cloneable
			? NonReactive<T>
			: T extends { toJSON(): infer R }
				? R
				: T extends readonly unknown[]
					? { [K in keyof T]: Snapshot<T[K]> }
					: T extends Array<infer U>
						? Array<Snapshot<U>>
						: T extends object
							? T extends { [key: string]: any }
								? { [K in keyof T]: Snapshot<T[K]> }
								: never
							: never;

It first checks if T is a Primitive, which is defined as such:

type Primitive = string | number | boolean | null | undefined;

It then checks if T is a Cloneable, which is defined as such:

	/** The things that `structuredClone` can handle — https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm */
	export type Cloneable =
		| ArrayBuffer
		| DataView
		| Date
		| Error
		| Map<any, any>
		| RegExp
		| Set<any>
		| TypedArray
		// web APIs
		| Blob
		| CryptoKey
		| DOMException
		| DOMMatrix
		| DOMMatrixReadOnly
		| DOMPoint
		| DOMPointReadOnly
		| DOMQuad
		| DOMRect
		| DOMRectReadOnly
		| File
		| FileList
		| FileSystemDirectoryHandle
		| FileSystemFileHandle
		| FileSystemHandle
		| ImageBitmap
		| ImageData
		| RTCCertificate
		| VideoFrame;

BigInts are not only supported by structuredClone, they are also primitives. The types for $state.snapshot should reflect that BigInts will work. Adding bigint to Primitive will resolve the issue.

Reproduction

The following code:

const object = $state({
    number: 1n,
});

const snapshot = $state.snapshot(object);

The type of snapshot.number is inferred as never, despite being properly cloned as a BigInt.

Image

The value however is indeed 1: https://svelte.dev/playground/398a7224d0c44d03a04f39ce15b9662a?version=latest

Logs

System Info

System:
    OS: macOS 26.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 91.11 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.16.0 - /Users/nick/.nvm/versions/node/v24.16.0/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 11.16.0 - /Users/nick/.nvm/versions/node/v24.16.0/bin/npm
    pnpm: 11.5.0 - /opt/homebrew/bin/pnpm
    bun: 1.3.14 - /opt/homebrew/bin/bun
    Deno: 2.8.1 - /opt/homebrew/bin/deno
  Browsers:
    Firefox: 151.0.3
    Firefox Developer Edition: 134.0
    Safari: 26.5
  npmPackages:
    svelte: ^5.56.1 => 5.56.1

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions