mfc
mfc

Reputation: 3026

Cannot match Request Options Header in typescript

I'm trying to use the Request package in Typescript, the method definition being :-

export function request(options?: Options, callback?: (error: any, response: any, body: any) => void): Request;

I have an issue trying to match the headers in the Options. The Options and Header definitions are :-

    export interface Options {
        url?: string;
        headers?: Headers;
        ...
        ...

    export interface Headers {
        [key: string]: any;
    }

My options looks like :-

var requestOptions = {
    url: 'https://www.wigglewoowoo.com',
    method: 'POST',
    headers: {
        'Connection': 'close'
    },
    body: returnBody,
    strictSSL: true,
    rejectUnauthorized: false,
    requestCert: true,
    agent: false
};

I get an "is not assignable to parameter of type Options" unless I exclude the header. I cannot see what's wrong with it?

Upvotes: 1

Views: 2489

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 221014

The best fix is to add a type annotation here (first line):

var requestOptions: Options = {
    url: 'https://www.wigglewoowoo.com',
    method: 'POST',
    headers: {
        'Connection': 'close'
    },
    ... ...

To understand why, see this long question/answer about how object literals and index signatures interact (the case here is slightly different, but the same general problem applies).

Upvotes: 2

Related Questions