Is it possible to accurately define a structure clonable type in TypeScript

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)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật