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

Type Narrowing Incorrect on Exported Variable #57817

Closed
ChrisMeyer7088 opened this issue Mar 17, 2024 · 2 comments
Closed

Type Narrowing Incorrect on Exported Variable #57817

ChrisMeyer7088 opened this issue Mar 17, 2024 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ChrisMeyer7088
Copy link

πŸ”Ž Search Terms

Narrowing Export

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about 5.0 and higher releases

⏯ Playground Link

https://www.typescriptlang.org/play?#code/DYUwLgBARsCGAWAuCBnMAnAlgOwOYQB8IBXbAExADMcQyBuAKAc0ogAoBCGBASgjHjoA9gHcI2EGICi6YejYByACrxMKCCFlD0EMkJDrsQyNlhaxYAJ4AHEAp5MQAD2vbIAbwYRoceBAD0-hAASuDE6NjqVraoGDj4RKQU1BJkADSo8ELEwGTQIOJmwiK0-EKxWHgMAL5AA

πŸ’» Code

let blah: string | undefined;

if (!blah) throw new Error('This error does not narrow type')

export {
  blah // Returns type string | undefined, should be narrowed to string
}

πŸ™ Actual behavior

Expected the throwing of an error to type narrow the exported variable, but it instead held onto its initial typing.

πŸ™‚ Expected behavior

Throwing an error should type narrow exported variables

Additional information about the issue

No response

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Mar 18, 2024

This is the correct behavior. It's possible to observe blah before the if, e.g.:

// self-import in foo.mjs
import { blah as self_blah } from "./foo.mjs";
let blah;
// prints "undefined"
console.log(self_blah);
if (!blah) throw new Error('This error does not narrow type')
export {
  blah
}

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 18, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants