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

Type 'string' is not assignable to type 'RequestCredentials' #21618

Closed
parthi82 opened this issue Feb 4, 2018 · 2 comments
Closed

Type 'string' is not assignable to type 'RequestCredentials' #21618

parthi82 opened this issue Feb 4, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@parthi82
Copy link

parthi82 commented Feb 4, 2018

TypeScript Version: 2.7.1

async function deleteUser(id:string) {
    const url = `/v1/users/${id}`;
    let reqObj = {
        method: 'DELETE',
        credentials: "same-origin"
    }
    try {
        const result = await fetch(url, reqObj);
        const body = await result.json();
        if (!body || !body.success) {
            return ;
        }
        return true;
    } catch (err) {
        console.log(err);
    }
}

Raises the following error:

TS2345: Argument of type '{ method: string; credentials: string; }' is not assignable to parameter of type 'RequestInit'.
  Types of property 'credentials' are incompatible.
    Type 'string' is not assignable to type 'RequestCredentials'.

But if we pass RequestInit object directly without assigning to a variable, like this:

 const result = await fetch(url, {
       method: 'DELETE',
       credentials: "same-origin"
  });

It doesn't raise any error

@aluanhaddad
Copy link
Contributor

When you pass it inline, the object literal is contextually typed by the signature of the fetch function. When you declare it beforehand there is no such context and especially given that the object property is mutable, it's type is inferred as string.

@ahejlsberg
Copy link
Member

Duplicate of #17363. Also see comment here #20271 (comment).

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Feb 5, 2018
@parthi82 parthi82 closed this as completed Feb 8, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants