Reputation: 669
Aka define the set of all possible values that can be passed through structured clone algorithm (mdn, html spec, structuredClone).
It'd be super useful to constrain types that has to be passed through the .postMessage
, structuredClone
, persisted via history.pushState
, or IndexedDB without the fear of encountering (and having to debug) DataCloneError
. Currently I'm in the middle of using it with IndexedDB
Kinda weird that it isn't built-in into typescript via the web family of APIs (it is technically defined not in the ecmascript spec, but in the html one, although is also present in Node, Deno, Bun, jsc, etc.) window/worker.postMessage
, history.pushState
and IndexedDB are all super old and widely available APIs. Maybe the average 'lil JS dev doesn't use those APIs (which is a shame tbh) and there isn't that much demand for it.
Currently I have a silly definition for it like this:
type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;
type StructureClonableError =
| Error
| EvalError
| RangeError
| ReferenceError
| SyntaxError
| TypeError
| URIError;
/** @link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#webapi_types */
type WebClonableObjects =
// | AudioData
| Blob
// | CropTarget
| CryptoKey
| DOMException
| DOMMatrix
| DOMMatrixReadOnly
| DOMPoint
| DOMPointReadOnly
| DOMQuad
| DOMRect
| DOMRectReadOnly
| File
| FileList
| FileSystemDirectoryHandle
| FileSystemFileHandle
| FileSystemHandle
// | GPUCompilationInfo
// | GPUCompilationMessage
| ImageBitmap
| ImageData
| RTCCertificate
| VideoFrame;
/** @link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm */
export type StructureClonable =
| string
| number
| boolean
| bigint
| null
| undefined
| StructureClonable[]
| readonly StructureClonable[]
| { [key: string]: StructureClonable }
| Set<StructureClonable>
| Map<StructureClonable, StructureClonable>
| Date
| ArrayBuffer
| StructureClonableError
| RegExp
| TypedArray
| WebClonableObjects;
function hasToBeStructureClonable(value: StructureClonable) { worker.postMessage(value) }
// shorthand
const htbsc = hasTobeStructureClonable
// Seems ok at first
htbsc({ foo: "bar" }) // true positive. ok
htbsc(document) // true negative. ok
htbsc({ element: document.createElement("div") }) // true negative. ok
htbsc(() => {}) // true negative. ok
htbsc({ [Symbol()]: "symbols are not ok" }) // true negative. ok
htbsc(new Date) // true positive. ok
htbsc(new class {}) // true negative. ok
htbsc(new Error) // true positive. ok
// But there are holes
htbsc(Object.assign(new Date, { document })) // false positive. not ok
// Non built-in prototypes are can't be serialized
htbsc(new class MyError extends Error {}) // false positive. not ok
htbsc(Object.assign([1,2,3], { document })) // false positive. not ok
htbsc({ get foo() { return "bar" } }) // well, kinda
Also structured clone ignores property descriptors, and removes write restrictions, but that's not that important. As a part of this getter/setter functions will also be dropped, meaning the resulting object may be of different shape. I don't think you can do anything with that, as typescript can't track whether or not property is stored or defined via a getter (which also breaks type guard inference and type-system soundness, but that's beside the point)
Upvotes: 0
Views: 75