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

Allow : { } / : any type annotations on catch variables #10000

Closed
RyanCavanaugh opened this issue Jul 28, 2016 · 5 comments
Closed

Allow : { } / : any type annotations on catch variables #10000

RyanCavanaugh opened this issue Jul 28, 2016 · 5 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@RyanCavanaugh
Copy link
Member

It was pointed out in #9999 that catch variables never get narrowed because they're of type any, and you can't change their type because they're not allowed to have type annotations.

This leads to pitfalls like

try {
  // do something
} catch(e) {
  if(e instanceof MyError) {
    console.log(e.filname); // oops.
  }
}

Proposal: allow type annotations on catch variables as long as the annotated type is exactly any or { } (we can't reasonably make this an implicit any according to --noImplicitAny so that behavior would be unchanged). But now "safer" developers can write

try {
  // do something
} catch(e: { }) {
  if(e instanceof MyError) {
    console.log(e.filname); // error, property not found
  }
  console.log(e.filename); // error, forgot to narrow with a guard
}

Alternatively (or in addition to), we could just let catch variables get narrowed despite being any, but this would be a fairly odd carve-out.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jul 28, 2016
@RyanCavanaugh
Copy link
Member Author

🎈 🎉 10,000th issue 🎉 🎈

@yortus
Copy link
Contributor

yortus commented Jul 28, 2016

Related discussion in #8677

@yortus
Copy link
Contributor

yortus commented Aug 1, 2016

we could just let catch variables get narrowed despite being any, but this would be a fairly odd carve-out.

You could also see it the other way, that type guards were originally intended to work everywhere, but #1433 created a special carve-out where type guards no longer work (for pragmatic reasons). In that light, having type guards still work in the catch clause, despite the any type, is actually reducing the existing carve out.

@yortus
Copy link
Contributor

yortus commented Aug 1, 2016

A consideration here is discoverability. The following gives no compiler errors, so the author may mistakenly believe that TypeScript is type-checking the code when it is actually not:

try {
    foo();
}
catch (ex) {
    if (ex instanceof FooError) {
        /* recover from FooError */
        ex.foo // NOT typechecked - won't find typos, won't be picked up in refactorings
        ...
    }
    else {
        throw ex;
    }
}

It seems a bit magical and arbitrary that if we take the same code, but change catch (ex) to either catch (ex: any) or catch (ex: {}), then it also compiles without errors but now provides additional type checking.

How would a user know that they need to add the annotation if the compiler doesn't complain either way? Often people don't realise there is something wrong with their code until they see a compile-time or runtime error, and with TypeScript the main benefit is to get more of the former and less of the latter.

@RyanCavanaugh RyanCavanaugh added Fixed A PR has been merged for this issue and removed In Discussion Not yet reached consensus labels Aug 12, 2016
@RyanCavanaugh
Copy link
Member Author

This will be handled be #9999

@mhegazy mhegazy added this to the TypeScript 2.0.1 milestone Aug 17, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants