Skip to content

Declaration emit writes = Infinity initializers for overflowing number literals (invalid .d.ts, TS1254) #64760

Open

Description

🔎 Search Terms

declaration emit Infinity, d.ts Infinity, TS1254 ambient const initializer, 1e400 declaration, number literal overflow declaration

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about declaration emit, Infinity and numeric literal types (none apply).
  • Tried: 6.0.3 (Playground) and main (7.1.0-dev, b5c34ba). Both emit the invalid = Infinity initializers; 6.0.3 also emits declare const _default: Infinity; for the default export.

⏯ Playground Link

https://www.typescriptlang.org/play/?declaration=true#src=export%20const%20a%20%3D%201e400%3B%0Aexport%20const%20b%20%3D%20-1e400%3B%0Aexport%20class%20C%20%7B%0A%20%20%20%20static%20readonly%20e%20%3D%201e400%3B%0A%7D%0Aexport%20default%201.7976931348623159e308%3B%0A

💻 Code

export const a = 1e400;
export const b = -1e400;
export class C {
    static readonly e = 1e400;
}
export default 1.7976931348623159e308;

🙁 Actual behavior

With --declaration, the emitted .d.ts is:

export declare const a = Infinity;
export declare const b = -Infinity;
export declare class C {
    static readonly e = Infinity;
}
declare const _default = Infinity;
export default _default;

Checking that file reports TS1254 on every initializer: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference." Infinity is an identifier, not a literal.

🙂 Expected behavior

The emitted .d.ts type-checks, for example:

export declare const a: number;
export declare const b: number;
export declare class C {
    static readonly e: number;
}
declare const _default: number;
export default _default;

Additional information about the issue

Related: #42905 (Infinity in type positions, fixed in 7.1), #63831 (-1e500 as const emitted as -Infinity) and #56272 (whether overflowing literals should have an Infinity literal type at all). This issue is narrower: whatever the type is, the emitted .d.ts should be valid.

I have a fix with a compiler test ready: https://github.com/sidcraftscode/TypeScript/tree/fix/dts-non-finite-literals. I'm opening a PR that references this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions