Skip to content

Generic types on lib.dom window fetch #44000

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

Closed
5 tasks done
caenguidanos opened this issue May 7, 2021 · 3 comments
Closed
5 tasks done

Generic types on lib.dom window fetch #44000

caenguidanos opened this issue May 7, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@caenguidanos
Copy link

caenguidanos commented May 7, 2021

Suggestion

Add generic types to window.fetch

πŸ” Search Terms

  • window.fetch
  • Response types
  • json() fetch types

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

// declare function fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
declare function fetch<T = any>(input: RequestInfo, init?: RequestInit): Promise<Response<T>>;

// interface Response extends Body {
interface Response<T = any> extends Body<T> {
    readonly headers: Headers;
    readonly ok: boolean;
    readonly redirected: boolean;
    readonly status: number;
    readonly statusText: string;
    readonly trailer: Promise<Headers>;
    readonly type: ResponseType;
    readonly url: string;
    clone(): Response;
}

interface Request extends Body { ... }

// interface Body {
interface Body<T = any> {
    readonly body: ReadableStream<Uint8Array> | null;
    readonly bodyUsed: boolean;
    arrayBuffer(): Promise<ArrayBuffer>;
    blob(): Promise<Blob>;
    formData(): Promise<FormData>;
    json(): Promise<T>;
    text(): Promise<string>;
}

πŸ“ƒ Motivating Example

type User = { user: string, password: string }

const response = await fetch<User>('https://...')

let user: User

if (response.ok) user = await response.json()

// Maybe better than ??
interface UserResponse extends Response {
  json(): Promise<User>
}

const response: UserResponse = await fetch('https://...')

@MartinJohns
Copy link
Contributor

MartinJohns commented May 7, 2021

That would just decrease type safety, as the user would be mistakenly lead to believe the type will be returned. This is the opposite of what TypeScript tries to add.

IMO the json() method should return a Promise<unknown>, not a Promise<any> or Promise<T>. But that would be a heavy breaking change.

@MartinJohns
Copy link
Contributor

This seems to be a duplicate of #33037.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 10, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants