Description
The FormData
constructor, as defined in the XHR spec, takes two optional parameters: the first of type HTMLFormElement
, and the second of type HTMLElement
. These are DOM APIs, which server-side runtimes don't support.
The way I see it, if any of these arguments is passed (and it's anything other than undefined
), this indicates a bug in user code. For example, the code in question could be isomorphic code using DOM APIs through a library such as jsdom or deno-dom, and the developer mistakenly used those objects with the runtime's FormData
implementation. TypeScript might not catch that, since it could be set up to use the browser's type definitions rather than the runtime's.
In the runtimes I've tested:
- Node.js and Deno throw if the first argument is not
undefined
, but they ignore the second argument. - Cloudflare Workers /
workerd
, Bun and WinterJS ignore both arguments.
No runtimes seem to check the second argument. Specifying Node.js and Deno's behavior might be fine, since in browsers the second argument does nothing if the first one is undefined
, and the first argument is already checked. But the WebIDL implementation in browsers does check that the second argument is a valid HTMLElement
regardless of whether the first one is undefined
, so maybe we should be consistent with that. This would also let us specify this by just saying what happens with unsupported WebIDL types, which is something that might also apply to other APIs in the future.