I'm trying to create a function that wraps fetch from node-fetch by just adding a cookie:
import fetch from 'node-fetch';
const api = (path: string, params: RequestInit) => fetch(
path, {
...(params || {}),
headers: {
...(params?.headers || {}),
cookie: 'mycookie'
},
}
)
I'm getting this error on headers:
(property) RequestInit.headers?: string[][] | Headers | {
[key: string]: string;
} | undefined
Type '{ cookie: string; append(name: string, value: string): void; delete(name: string): void; get(name: string): string | null; has(name: string): boolean; set(name: string, value: string): void; forEach(callbackfn: (value: string, key: string, parent: Headers) => void, thisArg?: any): void; } | { ...; } | { ...; }' is not assignable to type 'string[][] | Headers | { [key: string]: string; } | undefined'.
Type '{ cookie: string; append(name: string, value: string): void; delete(name: string): void; get(name: string): string | null; has(name: string): boolean; set(name: string, value: string): void; forEach(callbackfn: (value: string, key: string, parent: Headers) => void, thisArg?: any): void; }' is not assignable to type 'string[][] | Headers | { [key: string]: string; } | undefined'.
Type '{ cookie: string; append(name: string, value: string): void; delete(name: string): void; get(name: string): string | null; has(name: string): boolean; set(name: string, value: string): void; forEach(callbackfn: (value: string, key: string, parent: Headers) => void, thisArg?: any): void; }' is not assignable to type 'undefined'.ts(2322)
At the end it says it is not assignable to type 'undefined'. I thought if a value for a type is guaranteed to be one of it's union types, it shouldn't complain.