Skip to content

Commit b6ad43d

Browse files
committed
Better error for wrong return (: vs =>) in types
It's very ambiguous in expression position, so impossible to give a better message from the parser. For example: let f = (x: number) => number => x + 1; ~~ Should be ':' But the parser doesn't know that 'number' isn't an expression now.
1 parent fcc9823 commit b6ad43d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/compiler/parser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,16 @@ namespace ts {
22312231
else if (parseOptional(returnToken)) {
22322232
signature.type = parseTypeOrTypePredicate();
22332233
}
2234+
else if (context === SignatureContext.Type) {
2235+
const start = scanner.getTokenPos();
2236+
const length = scanner.getTextPos() - start;
2237+
const backwardToken = parseOptional(returnToken === SyntaxKind.ColonToken ? SyntaxKind.EqualsGreaterThanToken : SyntaxKind.ColonToken);
2238+
if (backwardToken) {
2239+
// This is easy to get backward, especially in type contexts, so parse the type anyway
2240+
signature.type = parseTypeOrTypePredicate();
2241+
parseErrorAtPosition(start, length, Diagnostics._0_expected, tokenToString(returnToken));
2242+
}
2243+
}
22342244
}
22352245

22362246
function parseParameterList(context: SignatureContext) {

0 commit comments

Comments
 (0)