Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signal is not nullable in types of RequestInit #2448

Closed
gebsh opened this issue Nov 21, 2023 · 0 comments · Fixed by #2455
Closed

signal is not nullable in types of RequestInit #2448

gebsh opened this issue Nov 21, 2023 · 0 comments · Fixed by #2455
Labels
bug Something isn't working fetch good first issue Good for newcomers Types Changes related to the TypeScript definitions

Comments

@gebsh
Copy link
Contributor

gebsh commented Nov 21, 2023

Bug Description

According to the Fetch standard, the signal property in RequestInit might be nullable. However, currently, @types/node that use typings from this package do not allow setting that property to null and cause TypeScript errors.

Reproducible By

  1. Create a new package with the following dependencies: @types/node and typescript.

  2. Create a tsconfig.json file with the following content:

    {
        "compilerOptions": {
            "lib": ["esnext"],
            "module": "node16",
            "strict": true
            "target": "esnext"
        },
        "files": ["./main.mts"]
    }
  3. Create a main.mts file with the following content:

    const response = await fetch('http://localhost:3000', { signal: null });
  4. Compile the project using tsc.

Expected Behavior

There should be no error and TypeScript should compile main.mts.

Logs & Screenshots

This produces the following error:

main.mts:1:57 - error TS2322: Type 'null' is not assignable to type 'AbortSignal | undefined'.

1 const response = await fetch('http://localhost:3000', { signal: null });

Environment

Node v18.18.2, @types/node v20.9.3 (with undici-types v5.26.5)

Additional context

This works fine when adding "dom" to the lib option in tsconfig.json (or when using @types/web) as the built-in DOM types allow setting signal to null:

// Other properties omitted for brevity.
interface RequestInit {
    /** An AbortSignal to set request's signal. */
    signal?: AbortSignal | null;
}

This error makes it impossible to write concise app-specific fetch wrappers:

async function sendFile(file: File, signal?: AbortSignal) {
    return fetch('http://localhost:3000/upload', {
        method: 'POST',
        body: file,
        signal: signal ?? null,
    });
}

undefined can be used instead of null as the right-hand side of the nullish coalescing operator in the preceding snippet, but only when the exactOptionalPropertyTypes TypeScript compiler option is disabled. When combined with this flag, I'm forced to create fetch options in a verbose way:

async function sendFile(file: File, signal?: AbortSignal) {
    const options: RequestInit = {
        method: 'POST',
        body: file,
    };

    if ( signal !== undefined ) {
        options.signal = signal;
    }

    return fetch('http://localhost:3000/upload', options);
}
@gebsh gebsh added the bug Something isn't working label Nov 21, 2023
@KhafraDev KhafraDev added good first issue Good for newcomers Types Changes related to the TypeScript definitions fetch labels Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fetch good first issue Good for newcomers Types Changes related to the TypeScript definitions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants