Skip to content

Commit 12eac68

Browse files
committed
feat: handle throw type in call expression
1 parent 862a4ac commit 12eac68

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,17 @@ namespace ts {
10531053
return diagnostic;
10541054
}
10551055

1056+
function errorByThrowType(location: Node | undefined, value: Type) {
1057+
let message = "Unknown";
1058+
if (value.flags & TypeFlags.ThrowType) {
1059+
value = (<ThrowType>value).value;
1060+
}
1061+
if (value.flags & TypeFlags.StringLiteral) {
1062+
message = (<StringLiteralType>value).value;
1063+
}
1064+
error(location, Diagnostics.Type_instantiated_results_in_a_throw_type_saying_Colon_0, message);
1065+
}
1066+
10561067
function addErrorOrSuggestion(isError: boolean, diagnostic: DiagnosticWithLocation) {
10571068
if (isError) {
10581069
diagnostics.add(diagnostic);
@@ -4494,7 +4505,7 @@ namespace ts {
44944505
return typeToTypeNodeHelper((<SubstitutionType>type).baseType, context);
44954506
}
44964507
if (type.flags & TypeFlags.ThrowType) {
4497-
return typeToTypeNodeHelper(errorType, context);
4508+
return typeToTypeNodeHelper(neverType, context);
44984509
}
44994510

45004511
return Debug.fail("Should be unreachable.");
@@ -15122,12 +15133,7 @@ namespace ts {
1512215133
}
1512315134
}
1512415135
if (flags & TypeFlags.ThrowType) {
15125-
const innerType = instantiateType((<ThrowType>type).value, mapper);
15126-
let errorMessage = "Unknown";
15127-
if (innerType.flags & TypeFlags.StringLiteral) {
15128-
errorMessage = (<StringLiteralType>innerType).value;
15129-
}
15130-
error(currentNode, Diagnostics.Type_instantiated_results_in_a_throw_type_saying_Colon_0, errorMessage);
15136+
errorByThrowType(currentNode, instantiateType((<ThrowType>type).value, mapper));
1513115137
return errorType;
1513215138
}
1513315139
return type;
@@ -27794,6 +27800,9 @@ namespace ts {
2779427800
}
2779527801

2779627802
const returnType = getReturnTypeOfSignature(signature);
27803+
if (returnType.flags & TypeFlags.ThrowType) {
27804+
errorByThrowType(node, returnType);
27805+
}
2779727806
// Treat any call to the global 'Symbol' function that is part of a const variable or readonly property
2779827807
// as a fresh unique symbol literal type.
2779927808
if (returnType.flags & TypeFlags.ESSymbolLike && isSymbolOrSymbolForCall(node)) {

0 commit comments

Comments
 (0)