export
presence of export narrowing typescript typecheck type checking export statement
https://www.typescriptlang.org/play/?ts=5.5.3#code/MYewdgzgLgBAHgLhgBQIYCcoEtUBsA8ASgKajoAm+YArgLYBGx6ANDAN6pLTpZgDmAXwB8QmAF52AgNwAoGbmKws4mAFZZWAGYAKOAG0sAXQCU7GTHgHDAOlQqA5AAtiuXCHuyLoSLE0r9RrIA9EEWYTAAegD8MgJyITBQ6ACeMNRgALSgtLTEYNj8iY5YEDC4vMQyCcRwAA4gmDDe0Glg6C6oUMTkACrFhRIALFJAA
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;
The type narrowing for an index type did not work unless an export was included.
The export should not affect the compilation or typechecking of a file at all.
This could possibly be a bug with just the TS playground; I did not test it on my machine.
As an additional note, if you change let to const, then there is no error. The problematic behavior only occurs with let.
let
const
Behavior is intentional; implemented in #57847, and see #58972
Hmmm, okay. This comment summarizes it well. Adding export actually encapsulates the file, making static analysis possible.
Activity
nullromo commentedon Jul 22, 2024
As an additional note, if you change
lettoconst, then there is no error. The problematic behavior only occurs withlet.jcalz commentedon Jul 22, 2024
Behavior is intentional; implemented in #57847, and see #58972
nullromo commentedon Jul 22, 2024
Hmmm, okay. This comment summarizes it well. Adding
exportactually encapsulates the file, making static analysis possible.