Skip to content

globalThis sometimes being stripped from type emition can leads to errors in emited definition #48783

Closed

Description

Bug Report

🔎 Search Terms

globalThis, removed, remove, stripped, 2502, own type annotation, referenced, definition, .d.ts

🕗 Version & Regression Information

  • This is the behavior in every version I tried, since 3.5.1, and I reviewed the FAQ for entries about globalThis, 2502, own type annotation

⏯ Playground Link

Playground link with relevant code, mainly using the D.TS tab

💻 Code

export const fetchSomething = (
	fetch: typeof globalThis.fetch
): typeof globalThis.fetch => fetch

🙁 Actual behavior

This code will generate the following .d.ts file, which contains the 'fetch' is referenced directly or indirectly in its own type annotation. (2502) error

export declare const fetchSomething: (fetch: typeof fetch) => typeof fetch;

🙂 Expected behavior

Generated d.ts file should keep the reference to globalThis

export declare const fetchSomething: (fetch: typeof globalThis.fetch) => typeof globalThis.fetch;

as it does when declaring a function, type or class:

export type FetchSomething = {
	fetch: typeof globalThis.fetch
}

export class FetchSomethingClass {
	fetch?: typeof globalThis.fetch
}

export type FetchSomethingFunction = (fetch: typeof globalThis.fetch) => typeof globalThis.fetch

// .d.ts
export declare type FetchSomething = {
    fetch: typeof globalThis.fetch;
};
export declare class FetchSomethingClass {
    fetch?: typeof globalThis.fetch;
}
export declare type FetchSomethingFunction = (fetch: typeof globalThis.fetch) => typeof globalThis.fetch;

🤔 Workaround

Typing the const instead of the anonymous function works as expected.

export const fetchSomething: (fetch: typeof globalThis.fetch) => typeof globalThis.fetch = (fetch) => fetch;

Activity

  1. jakebailey commented on Jun 21, 2022

    @jakebailey
    Member

    It looks like this applies to in any case where the function type is inferred from an arrow function (and probably function expression too?). The below shows a number of variations that break in d.ts emit, but also show the wrong thing in the tooltip (shown via // ^?). It also affects things if you have (foo: number, bar: typeof globalThis.foo) or something.

    Playground Link

    The main issue seems to be that we don't set the enclosingDeclaration to the arrow function when converting its signature to a type node; doing so almost fixes the bug but changes all sorts of other tests (honestly, many in positive ways, with a few confusing breaks). However, we then emit global.fetch (or similar) and not globalThis.fetch. The former errors out saying that global is not available.

    You can actually see this result if you declare something at the module level with the same name; then we do the right thing (because we see the arrow function's parent and decide to write it explicitly) except that globalThis becomes global which is weird. My example above shows this if the last line is uncommented.

  2. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions