-
Notifications
You must be signed in to change notification settings - Fork 448
Open
Labels
Description
Summary
Optional fields in lib.dom.d.ts
do not explicity have undefined
in their types, which makes their construction needlessly difficult
Expected vs. Actual Behavior
Expected following code to just work:
function fetchUsers(params?: { signal?: AbortSignal | undefined }) {
return fetch("/api/v1/user/", { signal: params?.signal });
}
But instead the following error is shown:
Argument of type '{ signal: AbortSignal | undefined; }' is not assignable to parameter of type 'RequestInit' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
Types of property 'signal' are incompatible.
Type 'AbortSignal | undefined' is not assignable to type 'AbortSignal | null'.
Type 'undefined' is not assignable to type 'AbortSignal | null'. (2379)
Following is required to be done instead (let's pretend that RequestInit.signal
cannot be set to null
):
function fetchUsers(params?: { signal?: AbortSignal | undefined }) {
const init: RequestInit = {};
if (params?.signal) {
init.signal = params.signal;
}
return fetch("/api/v1/user/", init);
}
Playground Link
Browser Support
- This API is supported in at least two major browser engines (not two Chromium-based browsers).
Have Tried The Latest Releases
- This issue applies to the latest release of TypeScript.
- This issue applies to the latest release of
@types/web
.
Additional Context
See https://webidl.spec.whatwg.org/#idl-dictionaries:
In the JavaScript binding, a value of undefined for the property corresponding to a dictionary member is treated the same as omitting that property.
I have not found a single | undefined
on an optional interface field in the entire lib.dom.d.ts
of Typescript 5.9.2.
jakebailey