apollo-fetch icon indicating copy to clipboard operation
apollo-fetch copied to clipboard

Readme example doesn't typecheck

Open ThomWright opened this issue 8 years ago • 0 comments

Intended outcome:

The middleware example in the readme should typecheck when used in Typescript:

apolloFetch.use(({ request, options }, next) => {
  if (!options.headers) {
    options.headers = {};  // Create the headers object if needed.
  }
  options.headers['authorization'] = 'created token';

  next();
});

Actual outcome:

An empty object cannot be assigned to options.headers:

message: 'Type '{}' is not assignable to type 'Headers | string[][] | undefined'.
  Type '{}' is not assignable to type 'string[][]'.
    Property 'includes' is missing in type '{}'.'

Apparently options.headers is either string[][] or Headers, where Headers seems to be (from the dom lib in the Typescript compiler options below):

interface Headers {
    append(name: string, value: string): void;
    delete(name: string): void;
    forEach(callback: ForEachCallback): void;
    get(name: string): string | null;
    has(name: string): boolean;
    set(name: string, value: string): void;
}

How to reproduce the issue:

Typescript compiler options:

{
  "compilerOptions": {
    "outDir": "./dist",
    "allowJs": true,
    "target": "es2017",
    "module": "commonjs",
    "lib": ["esnext", "esnext.asynciterable", "es2017", "dom"],
    "noUnusedLocals": true,
    "noErrorTruncation": true,
    "strict": true
  }
}

If there's anything else you need to be able to reproduce, let me know and I'll try to help out.

ThomWright avatar Nov 02 '17 14:11 ThomWright