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] Type of variable is widened in declaration with !strictNullChecks #57441

Open
dragomirtitian opened this issue Feb 19, 2024 · 1 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
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 declaration widen

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground Link

πŸ’» Code

declare function f3<T>(a: T, b: T): T;
const x3 = f3("abc", "def");  // "abc" | "def"

πŸ™ Actual behavior

x3 is emitted as declare const x3: string; in declaration files but it's actually "abc" | "def" in source

πŸ™‚ Expected behavior

x3 is emitted as declare const x3: "abc" | "def"

Additional information about the issue

No response

@fatcerberus
Copy link

fatcerberus commented Feb 19, 2024

I think this behavior might be connected with automatic literal widening because
Playground

declare function f3<T>(a: T, b: T): T;
const x4_1 = f3("abc", "def");  // "abc" | "def"
const x4_2 = f3("abc" as const, "def" as const);  // "abc" | "def"
let x5_1 = x4_1;
//  ^? string
let x5_2 = x4_2;
//  ^? "abc" | "def"

The behavior on x5_1 might seem weird but past input from maintainers has suggested it's intentional.

btw your playground link in the OP doesn't seem to work properly - for me it just loads whatever I had last entered into the playground.

@dragomirtitian dragomirtitian changed the title Type of variable is widened in declaration with !strictNullChecks [ID-Prep] Type of variable is widened in declaration with !strictNullChecks Feb 20, 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
@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

No branches or pull requests

4 participants