Skip to content

[ID-Prep] Preserve constant initializer in declarations as written in sourceΒ #57443

Closed

Description

The [ID-prep] set of issues aligns Declaration Emit with the forthcoming Isolated Declarations feature.

πŸ” Search Terms

const preserve initializer

βœ… Viability Checklist

⭐ Suggestion

TypeScript should only emit initializers in declaration files if the initializer is actually a literal type. Right now, TypeScript will emit an initializer for constants that are typed as a primitive literal type. This makes the emit behavior difficult to potentially emulate in other tools.

TypeScript should emit initializers in declaration files as written in the source file.

πŸ“ƒ Motivating Example

// index.ts
const octal = 0o16;
const hex = 0x10;
const seps = 1_000_000;
const singleQuote = 'x'
const noSubstTemplate = `Test`

// index-current.ts
declare const octal = 14;
declare const hex = 16;
declare const seps = 1000000;
declare const singleQuote = "x";
declare const noSubstTemplate = "Test";

// index-after.ts
declare const octal = 0o16;
declare const hex = 0x10;
declare const seps = 1_000_000;
declare const singleQuote = 'x'
declare const noSubstTemplate = `Test`

πŸ’» Use Cases

  1. What do you want to use this for?
    This would improvement would can bring us closer to external declaration emitters being a reality.
  2. What shortcomings exist with current approaches?
    We would need to detect the case of a primitive literal initializer anyway in order to make const bar = foo; an error with isolated declarations anyway. Preserving the original type node seems like a win for both source map and declaration fidelity.
  3. What workarounds are you using in the meantime?
    We could only raise the error and not change the current emit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Domain: Isolated DeclarationsRelated to the --isolatedDeclarations compiler flagHelp WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions