Repository navigation
Fix JSDoc type predicate inference for arrow functions - #65629
ABDALLA HASHIM (1abdala) wants to merge 2 commits into
Conversation
@microsoft-github-policy-service agree |
|
If you're willing, this would be much easier to review if it were two commits, one that adds the test and baselines showing bad behavior, then one commit with the fix and baseline update showing it works. Additionally, the name of this test should be replaced with something readable. |
Thanks for the feedback! That makes sense. I'll split the commits accordingly and rename the test to make it more readable. |
ef5a495 to
499efd3
Compare
Fixes #65549
Problem
In TypeScript 7 (native compiler), assigning a JSDoc type predicate via
@typeto an arrow function or function expression in a JavaScript file (e.g./** @type {(el: Node) => el is HTMLElement} */ const func1 = (el) => el instanceof HTMLElement && el.className === "foo";) failed with:TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. Signature '(el: Node): boolean' must be a type predicate.This regression occurred because:
getTypePredicateOfSignatureonly looked atsig.declaration.Type()and did not inspect JSDoc@typeannotations on enclosing declaration hosts in JavaScript files.node.FunctionLikeData().FullSignaturepopulated, leaving their return type unrecognized bygetReturnTypeFromAnnotationduring deferred body checking.Implementation
relater.go):getTypeTagTypeNode(decl)to resolve JSDoc@typetags on the function's declaration or supported enclosing declaration hosts (VariableDeclaration,PropertyAssignment,PropertyDeclaration, assignmentBinaryExpression,ExportAssignment, andFunctionDeclaration.FunctionLikeData().FullSignature).getTypePredicateOfSignature: whentypeNode == nilin a JS file, queriesgetTypeTagTypeNode(sig.declaration). If the tag specifies a function type with a type predicate return (el is HTMLElementorasserts el is HTMLElement), or a typedef alias, it resolves the type predicate.checker.go):getSignatureOfFullSignatureType: updated to checkgetTypeTagTypeNode(node)sogetReturnTypeFromAnnotationrecognizes the return type for arrow functions and function expressions in JS files, enabling deferred return-type checking (e.g. rejecting non-boolean returns).getSignaturesOfSymbol: guarded!ast.IsFunctionExpressionOrArrowFunction(decl) && !ast.IsObjectLiteralMethod(decl)before adopting the full signature from@type, directly implementing the existing comment at line 20575 and matching TypeScript 5/6 behavior. This ensures the declaration retains its own parameter signature so that parameter-name and position mismatches (e.g.(element: Node)vs(el: Node)) continue to be caught during assignability checks.Regression Coverage
Added
tsc/testdata/tests/cases/compiler/issue65549.tsand associated reference baselines covering 14 cases:&&(func1).instanceofbody (func2).@param/@returns(func3) andfunctiondeclaration (func4).func5).obj.isElement).C.isElement).func8a = ...).module.exports = ...).func10).func11).func8b-> TS2322).mismatch-> TS2322 parameter position mismatch).badReturn-> TS2322 number not assignable to boolean).Validation Performed
go test -run='TestLocal/^issue65549\.ts$' ./internal/testrunner(passed, matching baseline)jsdocTypeTagFunctionTypePredicate,inferTypePredicates,arrowFunctionJSDocAnnotation,typePredicateParameterMismatch,assertsPredicateParameterMismatch,checkJsdocTypeTagOnExportAssignment1(passed)go test ./internal/checker(passed)go build ./cmd/tsc(passed)