Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LeonardDrs opened this issue Apr 20, 2022 · 1 comment · Fixed by #49627
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@LeonardDrs
Copy link

LeonardDrs commented Apr 20, 2022

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;
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Apr 20, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.8.0 milestone Apr 20, 2022
@jakebailey
Copy link
Member

jakebailey commented Jun 21, 2022

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.

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jun 22, 2022
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
4 participants