Skip to content

Commit 9f1beab

Browse files
committed
feat: handle throw type in call expression
1 parent 8221170 commit 9f1beab

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.");
@@ -15126,12 +15137,7 @@ namespace ts {
1512615137
}
1512715138
}
1512815139
if (flags & TypeFlags.ThrowType) {
15129-
const innerType = instantiateType((<ThrowType>type).value, mapper);
15130-
let errorMessage = "Unknown";
15131-
if (innerType.flags & TypeFlags.StringLiteral) {
15132-
errorMessage = (<StringLiteralType>innerType).value;
15133-
}
15134-
error(currentNode, Diagnostics.Type_instantiated_results_in_a_throw_type_saying_Colon_0, errorMessage);
15140+
errorByThrowType(currentNode, instantiateType((<ThrowType>type).value, mapper));
1513515141
return errorType;
1513615142
}
1513715143
return type;
@@ -27798,6 +27804,9 @@ namespace ts {
2779827804
}
2779927805

2780027806
const returnType = getReturnTypeOfSignature(signature);
27807+
if (returnType.flags & TypeFlags.ThrowType) {
27808+
errorByThrowType(node, returnType);
27809+
}
2780127810
// Treat any call to the global 'Symbol' function that is part of a const variable or readonly property
2780227811
// as a fresh unique symbol literal type.
2780327812
if (returnType.flags & TypeFlags.ESSymbolLike && isSymbolOrSymbolForCall(node)) {

0 commit comments

Comments
 (0)