question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript: globalThis, Unsafe assignment of an `any` value

See original GitHub issue

Version 3.0.0-beta.9

I’m trying to patch the global object in typescript

import fetch from "node-fetch"

if (!globalThis.fetch) {
  globalThis.fetch = fetch
}

And I get this error

Type 'typeof fetch' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
  Types of parameters 'url' and 'input' are incompatible.
    Type 'RequestInfo' is not assignable to type 'import("node_modules/node-fetch/@types/index").RequestInfo'.
      Type 'Request' is not assignable to type 'RequestInfo'.
        Type 'Request' is missing the following properties from type 'Body': size, bufferts

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:8
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jimmywartingcommented, Jul 20, 2021

use top level await with es6 modules and conditionally load node-fetch instead

if (!globalThis.fetch) {
  globalThis.fetch = await import('node-fetch').then(m => m.default)
}

still complains? use // @ts-ignore

1reaction
LinusUcommented, Jan 18, 2022

Potentially something like this could work?

import fetch, { Headers, Request, Response } from 'node-fetch';

if (!('fetch' in globalThis)) {
  Object.assign(globalThis, { fetch, Headers, Request, Response })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using globalThis in Typescript - Stack Overflow
This turns this file into an ES module, which is necessary for this to work. You can export any of the statements or...
Read more >
The starting point for learning TypeScript
TypeScript Documentation. Get Started. Quick introductions based on your background or preference. TS for the New Programmer · TypeScript for JS ...
Read more >
Non-null assertions should not be used - SonarSource Rules
The point of declaring an optional property or parameter is to make explicit the fact that it might contain no valid value, i.e....
Read more >
Understanding TypeScript Configuration Options
Introducing how TypeScript configuration options work. TypeScript ... recursively matches any subdirectory ... TypeScript will detect the unsafe assignment:
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The value of obj.first is confirmed to be non- null (and non- undefined ) ... for the member bar in a map when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found