Skip to content
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

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

Closed
6 tasks done
dragomirtitian opened this issue Feb 19, 2024 · 1 comment
Closed
6 tasks done
Labels
Domain: Isolated Declarations Related to the --isolatedDeclarations compiler flag Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@dragomirtitian
Copy link
Contributor

dragomirtitian commented Feb 19, 2024

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.
@dragomirtitian dragomirtitian changed the title Only emit constant initializer in declarations if the initializer is a primitive literal. [ID-Prep] Only emit constant initializer in declarations if the initializer is a primitive literal. Feb 19, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Feb 20, 2024
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Feb 20, 2024
@dragomirtitian dragomirtitian changed the title [ID-Prep] Only emit constant initializer in declarations if the initializer is a primitive literal. [ID-Prep] Preserve constant initializer in declarations as written in source Mar 7, 2024
@dragomirtitian
Copy link
Contributor Author

As @weswigham pointed out the original goal of this PR would have changed the semantic meaning of the declarations.

The new goal is more modest in scope: Just preserve the original initializer where possible.

@weswigham weswigham added the Domain: Isolated Declarations Related to the --isolatedDeclarations compiler flag label May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Isolated Declarations Related to the --isolatedDeclarations compiler flag Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants