Skip to content

Allow unknown type annotation on catch clause variable #36775

Closed
@yokomotod

Description

@yokomotod

Search Terms

catch clause unknown exception

Suggestion

Now any kind of type annotation on catch clauses is not allowed.
In my understanding, it's because that it's unsafe to annotate like catch (err: SpecialError) since any value will be captured actually.

However, due to the type of err is any, it's also not type safe.

So I suggest to allow annotating unknown , as most safe typing.

(And annotation with any other type won't be allowed as is)

Examples

try {
  throw 42;
} catch (err) {
  console.error(err.specialFunction()); 
}

can be written safer as

try {
  throw 42;
} catch (err: unknown) {
  if (err instanceof SpecialError) {
    console.error(err.specialFunction()); 
  } else {
    ...
  }
}

Related Issue

#20024

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    This wouldn't change the runtime behavior of existing JavaScript code
    This could be implemented without emitting different JS based on the types of the expressions
    This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
    This feature would agree with the rest of TypeScript's Design Goals.

Activity

phiresky

phiresky commented on Mar 23, 2020

@phiresky

This would be great (together with a lint that forces doing this always).

Right now, catch (e) {} is basically a situation that should be forbidden by noImplicitAny, except it can't be because you can't annotate it with any type (except for unknown) since that would be misleading.

bradzacher

bradzacher commented on Mar 27, 2020

@bradzacher
Contributor

I would really love this as a compiler option.
Being able to set a flag that makes all caught exceptions unknown instead of manually annotating them would be much better, but I guess it would be harder to migrate to.

hediet

hediet commented on Mar 29, 2020

@hediet
Member

I think I would also prefer a new strict* compiler option over new syntax (that would need to be enforced with a lint rule).

In my understanding, it's because that it's unsafe to annotate like catch (err: SpecialError) since any value will be captured actually.

I think type safety is not the only issue here. Unexperienced TypeScript developers would think that this catch statement might only handle errors of type SpecialError!

Raynos

Raynos commented on May 5, 2020

@Raynos

👍 this would be amazing. To allow a type annotation on catch but only if its err: unknown

added
CommittedThe team has roadmapped this issue
and removed on May 5, 2020
added this to the TypeScript 4.0 milestone on May 5, 2020
MicahZoltu

MicahZoltu commented on May 9, 2020

@MicahZoltu
Contributor

Assuming this is the appropriate place to discuss this, I would like to advocate that the change in 4.0 be either (in order of preference):

  1. catch (error) results in error having a type of unknown
  2. Add a new strict option that makes error be of type unknown
  3. Add a new strict option that requires the user to do catch (error: unknown)

I understand that error is any for legacy reasons (it was before unknown existed) and therefore can't be trivially changed (at least without a major version bump). However, I would like to see a path that allows us to get away from that eventually, either with a breaking change (error is unknown) or with a new compiler setting that is flipped on with the strict flag.

Raynos

Raynos commented on May 9, 2020

@Raynos

I am excited typescript 4.0 will address this issue.

We had to add a specific --ignore-catch flag to the type-coverage command ( https://github.com/plantain-00/type-coverage#ignore-catch ) to handle the fact that catch {} is the only place an implicit any is mandatory and unavoidable.

ExE-Boss

ExE-Boss commented on May 31, 2020

@ExE-Boss
Contributor

It should probably also be possible to do:

try {
	// code
} catch (err: any) {
	// error handling
}

In case unknown ever becomes the default inferred type (e.g.: #27265).

52 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

CommittedThe team has roadmapped this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @elibarzilay@Raynos@yokomotod@MicahZoltu@WORMSS

    Issue actions

      Allow `unknown` type annotation on catch clause variable · Issue #36775 · microsoft/TypeScript