- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.1k
          Allow e: unknown in catch arguments
          #39015
        
          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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            61 changes: 54 additions & 7 deletions
          
          61 
        
  tests/baselines/reference/catchClauseWithTypeAnnotation.errors.txt
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,9 +1,56 @@ | ||
| tests/cases/compiler/catchClauseWithTypeAnnotation.ts(2,13): error TS1196: Catch clause variable cannot have a type annotation. | ||
| tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(17,36): error TS2339: Property 'foo' does not exist on type 'unknown'. | ||
| tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(18,37): error TS2339: Property 'foo' does not exist on type 'unknown'. | ||
| tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(19,23): error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified. | ||
| tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(20,23): error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified. | ||
| tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(29,29): error TS2492: Cannot redeclare identifier 'x' in catch clause. | ||
| tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts(30,29): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'boolean', but here has type 'string'. | ||
|  | ||
|  | ||
| ==== tests/cases/compiler/catchClauseWithTypeAnnotation.ts (1 errors) ==== | ||
| try { | ||
| } catch (e: any) { | ||
| ~~~ | ||
| !!! error TS1196: Catch clause variable cannot have a type annotation. | ||
| } | ||
| ==== tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts (6 errors) ==== | ||
| type any1 = any; | ||
| type unknown1 = unknown; | ||
|  | ||
| function fn(x: boolean) { | ||
|  | ||
| // no type annotation allowed other than `any` and `unknown` | ||
| try { } catch (x) { } // should be OK | ||
| try { } catch (x: any) { } // should be OK | ||
| try { } catch (x: any1) { } // should be OK | ||
| try { } catch (x: unknown) { } // should be OK | ||
| try { } catch (x: unknown1) { } // should be OK | ||
| try { } catch (x) { x.foo; } // should be OK | ||
| try { } catch (x: any) { x.foo; } // should be OK | ||
| try { } catch (x: any1) { x.foo; } // should be OK | ||
| try { } catch (x: unknown) { console.log(x); } // should be OK | ||
| try { } catch (x: unknown1) { console.log(x); } // should be OK | ||
| try { } catch (x: unknown) { x.foo; } // error in the body | ||
| ~~~ | ||
| !!! error TS2339: Property 'foo' does not exist on type 'unknown'. | ||
| try { } catch (x: unknown1) { x.foo; } // error in the body | ||
| ~~~ | ||
| !!! error TS2339: Property 'foo' does not exist on type 'unknown'. | ||
| try { } catch (x: Error) { } // error in the type | ||
| ~~~~~ | ||
| !!! error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified. | ||
| try { } catch (x: object) { } // error in the type | ||
| ~~~~~~ | ||
| !!! error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified. | ||
|  | ||
| try { console.log(); } | ||
| // @ts-ignore | ||
| catch (e: number) { // e should not be a `number` | ||
| console.log(e.toLowerCase()); | ||
| } | ||
|  | ||
| // minor bug: shows that the `catch` argument is skipped when checking scope | ||
| try { } catch (x) { let x: string; } | ||
| ~ | ||
| !!! error TS2492: Cannot redeclare identifier 'x' in catch clause. | ||
| try { } catch (x) { var x: string; } | ||
| ~ | ||
| !!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'boolean', but here has type 'string'. | ||
| !!! related TS6203 tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts:4:13: 'x' was also declared here. | ||
| try { } catch (x) { var x: boolean; } | ||
|  | ||
| } | ||
|  | 
        
          
          
            104 changes: 98 additions & 6 deletions
          
          104 
        
  tests/baselines/reference/catchClauseWithTypeAnnotation.js
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,10 +1,102 @@ | ||
| //// [catchClauseWithTypeAnnotation.ts] | ||
| try { | ||
| } catch (e: any) { | ||
| } | ||
| type any1 = any; | ||
| type unknown1 = unknown; | ||
|  | ||
| function fn(x: boolean) { | ||
|  | ||
| // no type annotation allowed other than `any` and `unknown` | ||
| try { } catch (x) { } // should be OK | ||
| try { } catch (x: any) { } // should be OK | ||
| try { } catch (x: any1) { } // should be OK | ||
| try { } catch (x: unknown) { } // should be OK | ||
| try { } catch (x: unknown1) { } // should be OK | ||
| try { } catch (x) { x.foo; } // should be OK | ||
| try { } catch (x: any) { x.foo; } // should be OK | ||
| try { } catch (x: any1) { x.foo; } // should be OK | ||
| try { } catch (x: unknown) { console.log(x); } // should be OK | ||
| try { } catch (x: unknown1) { console.log(x); } // should be OK | ||
| try { } catch (x: unknown) { x.foo; } // error in the body | ||
| try { } catch (x: unknown1) { x.foo; } // error in the body | ||
| try { } catch (x: Error) { } // error in the type | ||
| try { } catch (x: object) { } // error in the type | ||
|  | ||
| try { console.log(); } | ||
| // @ts-ignore | ||
| catch (e: number) { // e should not be a `number` | ||
| console.log(e.toLowerCase()); | ||
| } | ||
|  | ||
| // minor bug: shows that the `catch` argument is skipped when checking scope | ||
| try { } catch (x) { let x: string; } | ||
| try { } catch (x) { var x: string; } | ||
| try { } catch (x) { var x: boolean; } | ||
|  | ||
| } | ||
|  | ||
|  | ||
| //// [catchClauseWithTypeAnnotation.js] | ||
| try { | ||
| } | ||
| catch (e) { | ||
| function fn(x) { | ||
| // no type annotation allowed other than `any` and `unknown` | ||
| try { } | ||
| catch (x) { } // should be OK | ||
| try { } | ||
| catch (x) { } // should be OK | ||
| try { } | ||
| catch (x) { } // should be OK | ||
| try { } | ||
| catch (x) { } // should be OK | ||
| try { } | ||
| catch (x) { } // should be OK | ||
| try { } | ||
| catch (x) { | ||
| x.foo; | ||
| } // should be OK | ||
| try { } | ||
| catch (x) { | ||
| x.foo; | ||
| } // should be OK | ||
| try { } | ||
| catch (x) { | ||
| x.foo; | ||
| } // should be OK | ||
| try { } | ||
| catch (x) { | ||
| console.log(x); | ||
| } // should be OK | ||
| try { } | ||
| catch (x) { | ||
| console.log(x); | ||
| } // should be OK | ||
| try { } | ||
| catch (x) { | ||
| x.foo; | ||
| } // error in the body | ||
| try { } | ||
| catch (x) { | ||
| x.foo; | ||
| } // error in the body | ||
| try { } | ||
| catch (x) { } // error in the type | ||
| try { } | ||
| catch (x) { } // error in the type | ||
| try { | ||
| console.log(); | ||
| } | ||
| // @ts-ignore | ||
| catch (e) { // e should not be a `number` | ||
| console.log(e.toLowerCase()); | ||
| } | ||
| // minor bug: shows that the `catch` argument is skipped when checking scope | ||
| try { } | ||
| catch (x) { | ||
| var x_1; | ||
| } | ||
| try { } | ||
| catch (x) { | ||
| var x; | ||
| } | ||
| try { } | ||
| catch (x) { | ||
| var x; | ||
| } | ||
| } | 
        
          
          
            107 changes: 103 additions & 4 deletions
          
          107 
        
  tests/baselines/reference/catchClauseWithTypeAnnotation.symbols
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,104 @@ | ||
| === tests/cases/compiler/catchClauseWithTypeAnnotation.ts === | ||
| try { | ||
| } catch (e: any) { | ||
| >e : Symbol(e, Decl(catchClauseWithTypeAnnotation.ts, 1, 9)) | ||
| === tests/cases/conformance/statements/tryStatements/catchClauseWithTypeAnnotation.ts === | ||
| type any1 = any; | ||
| >any1 : Symbol(any1, Decl(catchClauseWithTypeAnnotation.ts, 0, 0)) | ||
|  | ||
| type unknown1 = unknown; | ||
| >unknown1 : Symbol(unknown1, Decl(catchClauseWithTypeAnnotation.ts, 0, 16)) | ||
|  | ||
| function fn(x: boolean) { | ||
| >fn : Symbol(fn, Decl(catchClauseWithTypeAnnotation.ts, 1, 24)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 3, 12), Decl(catchClauseWithTypeAnnotation.ts, 29, 27), Decl(catchClauseWithTypeAnnotation.ts, 30, 27)) | ||
|  | ||
| // no type annotation allowed other than `any` and `unknown` | ||
| try { } catch (x) { } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 6, 19)) | ||
|  | ||
| try { } catch (x: any) { } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 7, 19)) | ||
|  | ||
| try { } catch (x: any1) { } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 8, 19)) | ||
| >any1 : Symbol(any1, Decl(catchClauseWithTypeAnnotation.ts, 0, 0)) | ||
|  | ||
| try { } catch (x: unknown) { } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 9, 19)) | ||
|  | ||
| try { } catch (x: unknown1) { } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 10, 19)) | ||
| >unknown1 : Symbol(unknown1, Decl(catchClauseWithTypeAnnotation.ts, 0, 16)) | ||
|  | ||
| try { } catch (x) { x.foo; } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 11, 19)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 11, 19)) | ||
|  | ||
| try { } catch (x: any) { x.foo; } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 12, 19)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 12, 19)) | ||
|  | ||
| try { } catch (x: any1) { x.foo; } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 13, 19)) | ||
| >any1 : Symbol(any1, Decl(catchClauseWithTypeAnnotation.ts, 0, 0)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 13, 19)) | ||
|  | ||
| try { } catch (x: unknown) { console.log(x); } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 14, 19)) | ||
| >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
| >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
| >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 14, 19)) | ||
|  | ||
| try { } catch (x: unknown1) { console.log(x); } // should be OK | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 15, 19)) | ||
| >unknown1 : Symbol(unknown1, Decl(catchClauseWithTypeAnnotation.ts, 0, 16)) | ||
| >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
| >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
| >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 15, 19)) | ||
|  | ||
| try { } catch (x: unknown) { x.foo; } // error in the body | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 16, 19)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 16, 19)) | ||
|  | ||
| try { } catch (x: unknown1) { x.foo; } // error in the body | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 17, 19)) | ||
| >unknown1 : Symbol(unknown1, Decl(catchClauseWithTypeAnnotation.ts, 0, 16)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 17, 19)) | ||
|  | ||
| try { } catch (x: Error) { } // error in the type | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 18, 19)) | ||
| >Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|  | ||
| try { } catch (x: object) { } // error in the type | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 19, 19)) | ||
|  | ||
| try { console.log(); } | ||
| >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
| >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
| >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
|  | ||
| // @ts-ignore | ||
| catch (e: number) { // e should not be a `number` | ||
| >e : Symbol(e, Decl(catchClauseWithTypeAnnotation.ts, 23, 11)) | ||
|  | ||
| console.log(e.toLowerCase()); | ||
| >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
| >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
| >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
| >e : Symbol(e, Decl(catchClauseWithTypeAnnotation.ts, 23, 11)) | ||
| } | ||
|  | ||
| // minor bug: shows that the `catch` argument is skipped when checking scope | ||
| try { } catch (x) { let x: string; } | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 28, 19)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 28, 27)) | ||
|  | ||
| try { } catch (x) { var x: string; } | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 29, 19)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 3, 12), Decl(catchClauseWithTypeAnnotation.ts, 29, 27), Decl(catchClauseWithTypeAnnotation.ts, 30, 27)) | ||
|  | ||
| try { } catch (x) { var x: boolean; } | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 30, 19)) | ||
| >x : Symbol(x, Decl(catchClauseWithTypeAnnotation.ts, 3, 12), Decl(catchClauseWithTypeAnnotation.ts, 29, 27), Decl(catchClauseWithTypeAnnotation.ts, 30, 27)) | ||
|  | ||
| } | ||
|  | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.