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.
This commit is contained in:
Nathan Shively-Sanders
2017-07-11 10:08:42 -07:00
parent fcc9823ac7
commit b6ad43d4a5

View File

@@ -2231,6 +2231,16 @@ namespace ts {
else if (parseOptional(returnToken)) {
signature.type = parseTypeOrTypePredicate();
}
else if (context === SignatureContext.Type) {
const start = scanner.getTokenPos();
const length = scanner.getTextPos() - start;
const backwardToken = parseOptional(returnToken === SyntaxKind.ColonToken ? SyntaxKind.EqualsGreaterThanToken : SyntaxKind.ColonToken);
if (backwardToken) {
// This is easy to get backward, especially in type contexts, so parse the type anyway
signature.type = parseTypeOrTypePredicate();
parseErrorAtPosition(start, length, Diagnostics._0_expected, tokenToString(returnToken));
}
}
}
function parseParameterList(context: SignatureContext) {