TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

typeof with identically named type and value causes invalid .d.ts output

Open ssalbdivad opened this issue 1 year ago • 7 comments

🔎 Search Terms

type, value, .d.ts, circular, circularity, invalid, output, declaration

🕗 Version & Regression Information

  • This changed between versions 5.4 and 5.5

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.0-dev.20240501#code/KYDwDg9gTgLgBAYwgOwM7wCbFQqBLMGPFVOAXjgG8AoOOAMwggC44AiAIwEMo3qBfatVCRYcGAE8wwOFhz5CxNOXFTgEerOy4CREkA

💻 Code

export const descriptions = {
  foo: "bar"
}

export type descriptions = typeof descriptions

🙁 Actual behavior

Actual .d.ts output:

export declare const descriptions: typeof descriptions;
export type descriptions = typeof descriptions;

🙂 Expected behavior

Expected .d.ts output:

export declare const descriptions: {
    foo: string;
};
export type descriptions = typeof descriptions;

Additional information about the issue

No response

ssalbdivad avatar May 01 '24 19:05 ssalbdivad

Bisects to https://github.com/microsoft/TypeScript/pull/57772

Andarist avatar May 01 '24 20:05 Andarist

@RyanCavanaugh Is this not worth patching into TS 5.5?

Given how easy this is to repro, I'd assume this would occur often in the wild.

Issues like this that are only exposed in build output are also particularly insidious because most library authors don't explicitly check their build output, and most consumers use skipLibCheck, which would result in the affected type silently resolving to any.

ssalbdivad avatar May 02 '24 00:05 ssalbdivad

This would be quite a disaster especially considering this only occurs in the .d.ts which for libraries are often generated just before publishing.

patroza avatar May 02 '24 05:05 patroza

Following as Effect may be affected

mikearnaldi avatar May 02 '24 05:05 mikearnaldi

Given that this was introduced before release wouldn't it be a better strategy to revert the commit that introduced the bug and defer that commit to 5.6? It feels a bit odd to introduce a bug, recognize that it is a bug and defer the fix till a further version. I would understand the choice if this didn't came up pre-release

mikearnaldi avatar May 02 '24 06:05 mikearnaldi

Hmm this matches a ton of common behavior, even the humble as const "enum" is affected.

export const Color = {
  Red: "Red",
  Green: "Green",
  Blue: "Blue"
} as const

export type Color = typeof Color
export type Colors = Color[keyof Color]

This being broked^ though this works fine:

export const Color = {
  Red: "Red",
  Green: "Green",
  Blue: "Blue"
} as const

export type Color = typeof Color[keyof typeof Color]

hmmmm

datner avatar May 02 '24 07:05 datner

Yeah, good points all around

RyanCavanaugh avatar May 02 '24 15:05 RyanCavanaugh