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

Presence of export affects narrowing #59393

Closed
nullromo opened this issue Jul 22, 2024 · 3 comments
Closed

Presence of export affects narrowing #59393

nullromo opened this issue Jul 22, 2024 · 3 comments

Comments

@nullromo
Copy link

nullromo commented Jul 22, 2024

πŸ”Ž Search Terms

presence of export narrowing typescript typecheck type checking export statement

πŸ•— Version & Regression Information

  • This changed between versions 5.4.5 and 5.5.3

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.3#code/MYewdgzgLgBAHgLhgBQIYCcoEtUBsA8ASgKajoAm+YArgLYBGx6ANDAN6pLTpZgDmAXwB8QmAF52AgNwAoGbmKws4mAFZZWAGYAKOAG0sAXQCU7GTHgHDAOlQqA5AAtiuXCHuyLoSLE0r9RrIA9EEWYTAAegD8MgJyITBQ6ACeMNRgALSgtLTEYNj8iY5YEDC4vMQyCcRwAA4gmDDe0Glg6C6oUMTkACrFhRIALFJAA

πŸ’» Code

const x: Partial<Record<number, {a: string}>> = {};

let i = 5;
if(x[i]) {
  x[i].a = 'hello';
}

// try un-commenting this line
// export const unrelatedThing = 4;

πŸ™ Actual behavior

The type narrowing for an index type did not work unless an export was included.

πŸ™‚ Expected behavior

The export should not affect the compilation or typechecking of a file at all.

Additional information about the issue

This could possibly be a bug with just the TS playground; I did not test it on my machine.

@nullromo
Copy link
Author

As an additional note, if you change let to const, then there is no error. The problematic behavior only occurs with let.

@jcalz
Copy link
Contributor

jcalz commented Jul 22, 2024

Behavior is intentional; implemented in #57847, and see #58972

@nullromo
Copy link
Author

Hmmm, okay. This comment summarizes it well. Adding export actually encapsulates the file, making static analysis possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants