diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 83cb9045a03da..afd14ce97eedb 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)) { @@ -32140,7 +32140,7 @@ namespace ts { if (catchClause) { // Grammar checking if (catchClause.variableDeclaration) { - if (catchClause.variableDeclaration.type) { + 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 new file mode 100644 index 0000000000000..52b8d0978e9a8 --- /dev/null +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.js @@ -0,0 +1,19 @@ +//// [catchClauseWithUnknownTypeAnnotation.ts] +try { +} catch (e: unknown) { +} +type UnknownAlias = unknown; +try { +} catch (e: UnknownAlias) { +} + + +//// [catchClauseWithUnknownTypeAnnotation.js] +try { +} +catch (e) { +} +try { +} +catch (e) { +} diff --git a/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols new file mode 100644 index 0000000000000..e93472118c6f0 --- /dev/null +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.symbols @@ -0,0 +1,14 @@ +=== tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts === +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 new file mode 100644 index 0000000000000..123a5eb185aa7 --- /dev/null +++ b/tests/baselines/reference/catchClauseWithUnknownTypeAnnotation.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts === +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 new file mode 100644 index 0000000000000..cdf962b1384bf --- /dev/null +++ b/tests/cases/compiler/catchClauseWithUnknownTypeAnnotation.ts @@ -0,0 +1,7 @@ +try { +} catch (e: unknown) { +} +type UnknownAlias = unknown; +try { +} catch (e: UnknownAlias) { +}