Skip to content

Commit 2a9f067

Browse files
committed
feat: handle throw type in call expression
1 parent 1befac3 commit 2a9f067

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
@@ -1059,6 +1059,17 @@ namespace ts {
10591059
return diagnostic;
10601060
}
10611061

1062+
function errorByThrowType(location: Node | undefined, value: Type) {
1063+
let message = "Unknown";
1064+
if (value.flags & TypeFlags.ThrowType) {
1065+
value = (<ThrowType>value).value;
1066+
}
1067+
if (value.flags & TypeFlags.StringLiteral) {
1068+
message = (<StringLiteralType>value).value;
1069+
}
1070+
error(location, Diagnostics.Type_instantiated_results_in_a_throw_type_saying_Colon_0, message);
1071+
}
1072+
10621073
function addErrorOrSuggestion(isError: boolean, diagnostic: DiagnosticWithLocation) {
10631074
if (isError) {
10641075
diagnostics.add(diagnostic);
@@ -4525,7 +4536,7 @@ namespace ts {
45254536
return typeToTypeNodeHelper((<SubstitutionType>type).baseType, context);
45264537
}
45274538
if (type.flags & TypeFlags.ThrowType) {
4528-
return typeToTypeNodeHelper(errorType, context);
4539+
return typeToTypeNodeHelper(neverType, context);
45294540
}
45304541

45314542
return Debug.fail("Should be unreachable.");
@@ -15169,12 +15180,7 @@ namespace ts {
1516915180
}
1517015181
}
1517115182
if (flags & TypeFlags.ThrowType) {
15172-
const innerType = instantiateType((<ThrowType>type).value, mapper);
15173-
let errorMessage = "Unknown";
15174-
if (innerType.flags & TypeFlags.StringLiteral) {
15175-
errorMessage = (<StringLiteralType>innerType).value;
15176-
}
15177-
error(currentNode, Diagnostics.Type_instantiated_results_in_a_throw_type_saying_Colon_0, errorMessage);
15183+
errorByThrowType(currentNode, instantiateType((<ThrowType>type).value, mapper));
1517815184
return errorType;
1517915185
}
1518015186
return type;
@@ -27870,6 +27876,9 @@ namespace ts {
2787027876
}
2787127877

2787227878
const returnType = getReturnTypeOfSignature(signature);
27879+
if (returnType.flags & TypeFlags.ThrowType) {
27880+
errorByThrowType(node, returnType);
27881+
}
2787327882
// Treat any call to the global 'Symbol' function that is part of a const variable or readonly property
2787427883
// as a fresh unique symbol literal type.
2787527884
if (returnType.flags & TypeFlags.ESSymbolLike && isSymbolOrSymbolForCall(node)) {

0 commit comments

Comments
 (0)