From 1253c4e2d41ee5dc4068d1bdfefc4eac54c6f2fe Mon Sep 17 00:00:00 2001 From: yokomotod Date: Fri, 14 Feb 2020 00:27:56 +0900 Subject: [PATCH 1/3] Allow unknown type annotation on catch clause variable --- src/compiler/checker.ts | 2 +- .../reference/catchClauseWithUnknownTypeAnnotation.js | 11 +++++++++++ .../catchClauseWithUnknownTypeAnnotation.symbols | 6 ++++++ .../catchClauseWithUnknownTypeAnnotation.types | 6 ++++++ .../compiler/catchClauseWithUnknownTypeAnnotation.ts | 3 +++ 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js create mode 100644 tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols create mode 100644 tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types create mode 100644 tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 83cb9045a03da..d07f8d5f6d923 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32140,7 +32140,7 @@ namespace ts { if (catchClause) { // Grammar checking if (catchClause.variableDeclaration) { - if (catchClause.variableDeclaration.type) { + if (catchClause.variableDeclaration.type && catchClause.variableDeclaration.type.kind !== SyntaxKind.UnknownKeyword) { grammarErrorOnFirstToken(catchClause.variableDeclaration.type, Diagnostics.Catch_clause_variable_cannot_have_a_type_annotation); } else if (catchClause.variableDeclaration.initializer) { diff --git a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js new file mode 100644 index 0000000000000..2def4c5183fd3 --- /dev/null +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js @@ -0,0 +1,11 @@ +//// [catchClauseWithUnknownTypeAnnotation.ts] +try { +} catch (e: unknown) { +} + + +//// [catchClauseWithUnknownTypeAnnotation.js] +try { +} +catch (e) { +} diff --git a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols new file mode 100644 index 0000000000000..0c753c9647f8b --- /dev/null +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols @@ -0,0 +1,6 @@ +=== tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts === +try { +} catch (e: unknown) { +>e : Symbol(e, Decl(catchClauseWithUnknownTypeAnnotation.ts, 1, 9)) +} + diff --git a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types new file mode 100644 index 0000000000000..b717f09a9ebfa --- /dev/null +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types @@ -0,0 +1,6 @@ +=== tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts === +try { +} catch (e: unknown) { +>e : any +} + diff --git a/tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts b/tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts new file mode 100644 index 0000000000000..d3eaac5bcb0d5 --- /dev/null +++ b/tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts @@ -0,0 +1,3 @@ +try { +} catch (e: unknown) { +} From b589d147badf4dfb382e9931c24371050d3f7887 Mon Sep 17 00:00:00 2001 From: yokomotod Date: Sat, 15 Feb 2020 00:07:48 +0900 Subject: [PATCH 2/3] fix type of catch clause variable with unknown annotation --- src/compiler/checker.ts | 2 +- .../reference/catchClauseWithUnknownTypeAnnotation.types | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d07f8d5f6d923..112d3c52c401e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7688,7 +7688,7 @@ namespace ts { // Handle catch clause variables const declaration = symbol.valueDeclaration; if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) { - return anyType; + return tryGetTypeFromEffectiveTypeNode(declaration) === unknownType ? unknownType : anyType; } // Handle export default expressions if (isSourceFile(declaration) && isJsonSourceFile(declaration)) { diff --git a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types index b717f09a9ebfa..0d83a0f1e309f 100644 --- a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types @@ -1,6 +1,6 @@ === tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts === try { } catch (e: unknown) { ->e : any +>e : unknown } From 418820ffe79fbe7a5df675e12709d42fc7b5f989 Mon Sep 17 00:00:00 2001 From: yokomotod Date: Sat, 15 Feb 2020 00:47:22 +0900 Subject: [PATCH 3/3] fix type alias --- src/compiler/checker.ts | 2 +- .../reference/catchClauseWithUnknownTypeAnnotation.js | 8 ++++++++ .../catchClauseWithUnknownTypeAnnotation.symbols | 8 ++++++++ .../reference/catchClauseWithUnknownTypeAnnotation.types | 7 +++++++ .../compiler/catchClauseWithUnknownTypeAnnotation.ts | 4 ++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 112d3c52c401e..afd14ce97eedb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32140,7 +32140,7 @@ namespace ts { if (catchClause) { // Grammar checking if (catchClause.variableDeclaration) { - if (catchClause.variableDeclaration.type && catchClause.variableDeclaration.type.kind !== SyntaxKind.UnknownKeyword) { + if (catchClause.variableDeclaration.type && tryGetTypeFromEffectiveTypeNode(catchClause.variableDeclaration) !== unknownType) { grammarErrorOnFirstToken(catchClause.variableDeclaration.type, Diagnostics.Catch_clause_variable_cannot_have_a_type_annotation); } else if (catchClause.variableDeclaration.initializer) { diff --git a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js index 2def4c5183fd3..52b8d0978e9a8 100644 --- a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js @@ -2,6 +2,10 @@ try { } catch (e: unknown) { } +type UnknownAlias = unknown; +try { +} catch (e: UnknownAlias) { +} //// [catchClauseWithUnknownTypeAnnotation.js] @@ -9,3 +13,7 @@ try { } catch (e) { } +try { +} +catch (e) { +} diff --git a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols index 0c753c9647f8b..e93472118c6f0 100644 --- a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols @@ -3,4 +3,12 @@ try { } catch (e: unknown) { >e : Symbol(e, Decl(catchClauseWithUnknownTypeAnnotation.ts, 1, 9)) } +type UnknownAlias = unknown; +>UnknownAlias : Symbol(UnknownAlias, Decl(catchClauseWithUnknownTypeAnnotation.ts, 2, 1)) + +try { +} catch (e: UnknownAlias) { +>e : Symbol(e, Decl(catchClauseWithUnknownTypeAnnotation.ts, 5, 9)) +>UnknownAlias : Symbol(UnknownAlias, Decl(catchClauseWithUnknownTypeAnnotation.ts, 2, 1)) +} diff --git a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types index 0d83a0f1e309f..123a5eb185aa7 100644 --- a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types @@ -3,4 +3,11 @@ try { } catch (e: unknown) { >e : unknown } +type UnknownAlias = unknown; +>UnknownAlias : unknown + +try { +} catch (e: UnknownAlias) { +>e : unknown +} diff --git a/tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts b/tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts index d3eaac5bcb0d5..cdf962b1384bf 100644 --- a/tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts +++ b/tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts @@ -1,3 +1,7 @@ try { } catch (e: unknown) { } +type UnknownAlias = unknown; +try { +} catch (e: UnknownAlias) { +}