-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow type predicates as return types only #5992
Conversation
Also remove now-unused "Type predicate is only allowed as a return type" diagnostic.
return id; | ||
} | ||
}); | ||
const t = parseType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename t
to type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -2542,6 +2537,26 @@ namespace ts { | |||
return false; | |||
} | |||
|
|||
function parseTypeOrTypePredicate(): TypeNode { | |||
const typePredicateVariable = tryParse(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only call tryParse
if a call to isIdentifier()
is true. No point in incurring the tryParse
overhead if it isn't. Also, don't pass an arrow function to tryParse
. Instead, make a function called nextTokenIsIsKeyword
or some such and pass that. Avoids creating a function object on every invocation.
@ahejlsberg does this look good now? |
You might want to wait on @weswigham's work as a heads up. |
Also: 1. Remove notes I wrote myself for merging. 2. Switch to pattern matching on properties in a few places.
@DanielRosenwasser I merged to get Wesley's changes. The parser may need some work to forbid |
I'd like to get this in for 1.8. @ahejlsberg, @weswigham, can you take another look? |
case SyntaxKind.PropertySignature: | ||
case SyntaxKind.GetAccessor: | ||
return node === (node.parent as (PropertyDeclaration | GetAccessorDeclaration | PropertySignature)).type; | ||
function checkBindingPatternForTypePredicateVariable( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider checkIfTypePredicateVariableIsDeclaredInBindingPattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about isTypePredicateVariableDeclaredInBindingPattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that, but it also reports errors. I think that would still be fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I forgot about that -- check*
is a better prefix. (This is just a move of the code, so I hadn't looked too closely at the contents.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
checkBindingPatternForTypeVariable -> checkIfTypeVariableIsDeclaredInBindingPattern
@ahejlsberg can you take a look so I can merge this for 1.8? |
let typePredicateVariable: Identifier; | ||
if (isIdentifier()) { | ||
typePredicateVariable = tryParse(parseTypePredicatePrefix); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const typePredicateVariable = isIdentifier() && tryParse(parseTypePredicatePrefix);
👍 With my minor suggested change. |
…rn-types-only Allow type predicates as return types only
It does this in the parser instead of piecemeal in the checker. This means that the checker has some unused error checking code, so I took the opportunity to move the type predicate checking code into
checkTypePredicate
instead ofcheckSignatureDeclaration
where it was previously.Fixed #5903 and #5731. Obsoletes PRs #5936 and #5977.
@ahejlsberg and @weswigham, can you take a look?