From 4a7b0a7a022e7a8c55331b0705bc75b2d92353b7 Mon Sep 17 00:00:00 2001 From: 1abdala Date: Tue, 24 Nov 2026 08:56:15 +0800 Subject: [PATCH 1/2] Add regression test for JSDoc type predicate on arrow functions --- ...peTagFunctionTypePredicateArrow.errors.txt | 109 ++++++++ ...cTypeTagFunctionTypePredicateArrow.symbols | 169 +++++++++++++ ...docTypeTagFunctionTypePredicateArrow.types | 236 ++++++++++++++++++ .../jsdocTypeTagFunctionTypePredicateArrow.ts | 66 +++++ 4 files changed, 580 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.types create mode 100644 tsc/testdata/tests/cases/compiler/jsdocTypeTagFunctionTypePredicateArrow.ts diff --git a/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.errors.txt b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.errors.txt new file mode 100644 index 0000000000000..bff92e5cde17b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.errors.txt @@ -0,0 +1,109 @@ +index.js(2,7): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. + Signature '(el: Node): boolean' must be a type predicate. +index.js(19,7): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. + Signature '(el: Node): boolean' must be a type predicate. +index.js(25,5): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. + Signature '(el: Node): boolean' must be a type predicate. +index.js(30,5): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. + Signature '(el: Node): boolean' must be a type predicate. +index.js(41,1): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. + Signature '(el: Node): boolean' must be a type predicate. +index.js(44,1): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. + Signature '(el: Node): boolean' must be a type predicate. +index.js(48,7): error TS2322: Type '(el: Node) => boolean' is not assignable to type 'MyPredicate'. + Signature '(el: Node): boolean' must be a type predicate. +index.js(57,7): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(element: Node) => element is HTMLElement'. + Signature '(el: Node): boolean' must be a type predicate. +index.js(61,27): error TS2322: Type 'number' is not assignable to type 'boolean'. + + +==== index.js (9 errors) ==== + /** @type {(el: Node) => el is HTMLElement} */ + const func1 = (el) => el instanceof HTMLElement && el.className === "foo"; + ~~~~~ +!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. +!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. + + /** @type {(el: Node) => el is HTMLElement} */ + const func2 = (el) => el instanceof HTMLElement; + + /** + * @param {Node} el + * @returns {el is HTMLElement} + */ + const func3 = (el) => el instanceof HTMLElement && el.className === "foo"; + + /** @type {(el: Node) => el is HTMLElement} */ + function func4(el) { + return el instanceof HTMLElement && el.className === "foo"; + } + + /** @type {(el: Node) => el is HTMLElement} */ + const func5 = function(el) { + ~~~~~ +!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. +!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. + return el instanceof HTMLElement && el.className === "foo"; + }; + + const obj = { + /** @type {(el: Node) => el is HTMLElement} */ + isElement: (el) => el instanceof HTMLElement && el.className === "foo", + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. +!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. + }; + + class C { + /** @type {(el: Node) => el is HTMLElement} */ + isElement = (el) => el instanceof HTMLElement && el.className === "foo"; + ~~~~~~~~~ +!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. +!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. + } + + // Direct assignment with @type tag on the assignment expression + let func8a; + /** @type {(el: Node) => el is HTMLElement} */ + func8a = (el) => el instanceof HTMLElement && el.className === "foo"; + + // Tag on variable declaration, assignment separately: reports error matching TS 5/6 + /** @type {(el: Node) => el is HTMLElement} */ + let func8b; + func8b = (el) => el instanceof HTMLElement && el.className === "foo"; + ~~~~~~ +!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. +!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. + + /** @type {(el: Node) => el is HTMLElement} */ + module.exports = (el) => el instanceof HTMLElement && el.className === "foo"; + ~~~~~~~~~~~~~~ +!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. +!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. + + /** @typedef {(el: Node) => el is HTMLElement} MyPredicate */ + /** @type {MyPredicate} */ + const func10 = (el) => el instanceof HTMLElement && el.className === "foo"; + ~~~~~~ +!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type 'MyPredicate'. +!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. + + /** @type {(el: Node) => asserts el is HTMLElement} */ + const func11 = (el) => { + if (!(el instanceof HTMLElement)) throw new Error(); + }; + + // Parameter name mismatch: should report an error because parameter 'element' does not match 'el' + /** @type {(element: Node) => element is HTMLElement} */ + const mismatch = (el) => el instanceof HTMLElement && el.className === "foo"; + ~~~~~~~~ +!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(element: Node) => element is HTMLElement'. +!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. + + // Non-boolean return: should report an error because number is not assignable to boolean + /** @type {(el: Node) => el is HTMLElement} */ + const badReturn = (el) => 123; + ~~~ +!!! error TS2322: Type 'number' is not assignable to type 'boolean'. +!!! related TS6502 index.js:60:12: The expected type comes from the return type of this signature. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.symbols b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.symbols new file mode 100644 index 0000000000000..0d650aabd683b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.symbols @@ -0,0 +1,169 @@ +//// [tests/cases/compiler/jsdocTypeTagFunctionTypePredicateArrow.ts] //// + +=== index.js === +/** @type {(el: Node) => el is HTMLElement} */ +const func1 = (el) => el instanceof HTMLElement && el.className === "foo"; +>func1 : Symbol(func1, Decl(index.js, 1, 5)) +>el : Symbol(el, Decl(index.js, 1, 15)) +>el : Symbol(el, Decl(index.js, 1, 15)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 1, 15)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +/** @type {(el: Node) => el is HTMLElement} */ +const func2 = (el) => el instanceof HTMLElement; +>func2 : Symbol(func2, Decl(index.js, 4, 5)) +>el : Symbol(el, Decl(index.js, 4, 15)) +>el : Symbol(el, Decl(index.js, 4, 15)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) + +/** + * @param {Node} el + * @returns {el is HTMLElement} + */ +const func3 = (el) => el instanceof HTMLElement && el.className === "foo"; +>func3 : Symbol(func3, Decl(index.js, 10, 5)) +>el : Symbol(el, Decl(index.js, 10, 15)) +>el : Symbol(el, Decl(index.js, 10, 15)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 10, 15)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +/** @type {(el: Node) => el is HTMLElement} */ +function func4(el) { +>func4 : Symbol(func4, Decl(index.js, 10, 74)) +>el : Symbol(el, Decl(index.js, 13, 15)) + + return el instanceof HTMLElement && el.className === "foo"; +>el : Symbol(el, Decl(index.js, 13, 15)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 13, 15)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +} + +/** @type {(el: Node) => el is HTMLElement} */ +const func5 = function(el) { +>func5 : Symbol(func5, Decl(index.js, 18, 5)) +>el : Symbol(el, Decl(index.js, 18, 23)) + + return el instanceof HTMLElement && el.className === "foo"; +>el : Symbol(el, Decl(index.js, 18, 23)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 18, 23)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +}; + +const obj = { +>obj : Symbol(obj, Decl(index.js, 22, 5)) + + /** @type {(el: Node) => el is HTMLElement} */ + isElement: (el) => el instanceof HTMLElement && el.className === "foo", +>isElement : Symbol(isElement, Decl(index.js, 22, 13)) +>el : Symbol(el, Decl(index.js, 24, 16)) +>el : Symbol(el, Decl(index.js, 24, 16)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 24, 16)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +}; + +class C { +>C : Symbol(C, Decl(index.js, 25, 2)) + + /** @type {(el: Node) => el is HTMLElement} */ + isElement = (el) => el instanceof HTMLElement && el.className === "foo"; +>isElement : Symbol(C.isElement, Decl(index.js, 27, 9)) +>el : Symbol(el, Decl(index.js, 29, 17)) +>el : Symbol(el, Decl(index.js, 29, 17)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 29, 17)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +} + +// Direct assignment with @type tag on the assignment expression +let func8a; +>func8a : Symbol(func8a, Decl(index.js, 33, 3)) + +/** @type {(el: Node) => el is HTMLElement} */ +func8a = (el) => el instanceof HTMLElement && el.className === "foo"; +>func8a : Symbol(func8a, Decl(index.js, 33, 3)) +>el : Symbol(el, Decl(index.js, 35, 10)) +>el : Symbol(el, Decl(index.js, 35, 10)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 35, 10)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +// Tag on variable declaration, assignment separately: reports error matching TS 5/6 +/** @type {(el: Node) => el is HTMLElement} */ +let func8b; +>func8b : Symbol(func8b, Decl(index.js, 39, 3)) + +func8b = (el) => el instanceof HTMLElement && el.className === "foo"; +>func8b : Symbol(func8b, Decl(index.js, 39, 3)) +>el : Symbol(el, Decl(index.js, 40, 10)) +>el : Symbol(el, Decl(index.js, 40, 10)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 40, 10)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +/** @type {(el: Node) => el is HTMLElement} */ +module.exports = (el) => el instanceof HTMLElement && el.className === "foo"; +>module.exports : Symbol(exports, Decl(index.js, 0, 0)) +>module : Symbol(module, Decl(index.js, 0, 0)) +>exports : Symbol(exports, Decl(index.js, 0, 0)) +>el : Symbol(el, Decl(index.js, 43, 18)) +>el : Symbol(el, Decl(index.js, 43, 18)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 43, 18)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +/** @typedef {(el: Node) => el is HTMLElement} MyPredicate */ +/** @type {MyPredicate} */ +const func10 = (el) => el instanceof HTMLElement && el.className === "foo"; +>func10 : Symbol(func10, Decl(index.js, 47, 5)) +>el : Symbol(el, Decl(index.js, 47, 16)) +>el : Symbol(el, Decl(index.js, 47, 16)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 47, 16)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +/** @type {(el: Node) => asserts el is HTMLElement} */ +const func11 = (el) => { +>func11 : Symbol(func11, Decl(index.js, 50, 5)) +>el : Symbol(el, Decl(index.js, 50, 16)) + + if (!(el instanceof HTMLElement)) throw new Error(); +>el : Symbol(el, Decl(index.js, 50, 16)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) + +}; + +// Parameter name mismatch: should report an error because parameter 'element' does not match 'el' +/** @type {(element: Node) => element is HTMLElement} */ +const mismatch = (el) => el instanceof HTMLElement && el.className === "foo"; +>mismatch : Symbol(mismatch, Decl(index.js, 56, 5)) +>el : Symbol(el, Decl(index.js, 56, 18)) +>el : Symbol(el, Decl(index.js, 56, 18)) +>HTMLElement : Symbol(HTMLElement, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>el.className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) +>el : Symbol(el, Decl(index.js, 56, 18)) +>className : Symbol(Element.className, Decl(lib.dom.d.ts, --, --)) + +// Non-boolean return: should report an error because number is not assignable to boolean +/** @type {(el: Node) => el is HTMLElement} */ +const badReturn = (el) => 123; +>badReturn : Symbol(badReturn, Decl(index.js, 60, 5)) +>el : Symbol(el, Decl(index.js, 60, 19)) + diff --git a/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.types b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.types new file mode 100644 index 0000000000000..5aed7227f9fc3 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.types @@ -0,0 +1,236 @@ +//// [tests/cases/compiler/jsdocTypeTagFunctionTypePredicateArrow.ts] //// + +=== index.js === +/** @type {(el: Node) => el is HTMLElement} */ +const func1 = (el) => el instanceof HTMLElement && el.className === "foo"; +>func1 : (el: Node) => el is HTMLElement +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +/** @type {(el: Node) => el is HTMLElement} */ +const func2 = (el) => el instanceof HTMLElement; +>func2 : (el: Node) => el is HTMLElement +>(el) => el instanceof HTMLElement : (el: Node) => el is HTMLElement +>el : Node +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } + +/** + * @param {Node} el + * @returns {el is HTMLElement} + */ +const func3 = (el) => el instanceof HTMLElement && el.className === "foo"; +>func3 : (el: Node) => el is HTMLElement +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +/** @type {(el: Node) => el is HTMLElement} */ +function func4(el) { +>func4 : (el: Node) => el is HTMLElement +>el : Node + + return el instanceof HTMLElement && el.className === "foo"; +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" +} + +/** @type {(el: Node) => el is HTMLElement} */ +const func5 = function(el) { +>func5 : (el: Node) => el is HTMLElement +>function(el) { return el instanceof HTMLElement && el.className === "foo";} : (el: Node) => boolean +>el : Node + + return el instanceof HTMLElement && el.className === "foo"; +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +}; + +const obj = { +>obj : { isElement: (el: Node) => el is HTMLElement; } +>{ /** @type {(el: Node) => el is HTMLElement} */ isElement: (el) => el instanceof HTMLElement && el.className === "foo",} : { isElement: (el: Node) => el is HTMLElement; } + + /** @type {(el: Node) => el is HTMLElement} */ + isElement: (el) => el instanceof HTMLElement && el.className === "foo", +>isElement : (el: Node) => el is HTMLElement +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +}; + +class C { +>C : C + + /** @type {(el: Node) => el is HTMLElement} */ + isElement = (el) => el instanceof HTMLElement && el.className === "foo"; +>isElement : (el: Node) => el is HTMLElement +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" +} + +// Direct assignment with @type tag on the assignment expression +let func8a; +>func8a : any + +/** @type {(el: Node) => el is HTMLElement} */ +func8a = (el) => el instanceof HTMLElement && el.className === "foo"; +>func8a = (el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement +>func8a : any +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +// Tag on variable declaration, assignment separately: reports error matching TS 5/6 +/** @type {(el: Node) => el is HTMLElement} */ +let func8b; +>func8b : (el: Node) => el is HTMLElement + +func8b = (el) => el instanceof HTMLElement && el.className === "foo"; +>func8b = (el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>func8b : (el: Node) => el is HTMLElement +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +/** @type {(el: Node) => el is HTMLElement} */ +module.exports = (el) => el instanceof HTMLElement && el.className === "foo"; +>module.exports = (el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>module.exports : (el: Node) => el is HTMLElement +>module : { exports: (el: Node) => el is HTMLElement; } +>exports : (el: Node) => el is HTMLElement +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +/** @typedef {(el: Node) => el is HTMLElement} MyPredicate */ +/** @type {MyPredicate} */ +const func10 = (el) => el instanceof HTMLElement && el.className === "foo"; +>func10 : MyPredicate +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +/** @type {(el: Node) => asserts el is HTMLElement} */ +const func11 = (el) => { +>func11 : (el: Node) => asserts el is HTMLElement +>(el) => { if (!(el instanceof HTMLElement)) throw new Error();} : (el: Node) => void +>el : Node + + if (!(el instanceof HTMLElement)) throw new Error(); +>!(el instanceof HTMLElement) : boolean +>(el instanceof HTMLElement) : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>new Error() : Error +>Error : ErrorConstructor + +}; + +// Parameter name mismatch: should report an error because parameter 'element' does not match 'el' +/** @type {(element: Node) => element is HTMLElement} */ +const mismatch = (el) => el instanceof HTMLElement && el.className === "foo"; +>mismatch : (element: Node) => element is HTMLElement +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>el : Node +>el instanceof HTMLElement && el.className === "foo" : boolean +>el instanceof HTMLElement : boolean +>el : Node +>HTMLElement : { new (): HTMLElement; prototype: HTMLElement; } +>el.className === "foo" : boolean +>el.className : string +>el : HTMLElement +>className : string +>"foo" : "foo" + +// Non-boolean return: should report an error because number is not assignable to boolean +/** @type {(el: Node) => el is HTMLElement} */ +const badReturn = (el) => 123; +>badReturn : (el: Node) => el is HTMLElement +>(el) => 123 : (el: Node) => number +>el : Node +>123 : 123 + diff --git a/tsc/testdata/tests/cases/compiler/jsdocTypeTagFunctionTypePredicateArrow.ts b/tsc/testdata/tests/cases/compiler/jsdocTypeTagFunctionTypePredicateArrow.ts new file mode 100644 index 0000000000000..e41041ecbec20 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/jsdocTypeTagFunctionTypePredicateArrow.ts @@ -0,0 +1,66 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @filename: index.js +/** @type {(el: Node) => el is HTMLElement} */ +const func1 = (el) => el instanceof HTMLElement && el.className === "foo"; + +/** @type {(el: Node) => el is HTMLElement} */ +const func2 = (el) => el instanceof HTMLElement; + +/** + * @param {Node} el + * @returns {el is HTMLElement} + */ +const func3 = (el) => el instanceof HTMLElement && el.className === "foo"; + +/** @type {(el: Node) => el is HTMLElement} */ +function func4(el) { + return el instanceof HTMLElement && el.className === "foo"; +} + +/** @type {(el: Node) => el is HTMLElement} */ +const func5 = function(el) { + return el instanceof HTMLElement && el.className === "foo"; +}; + +const obj = { + /** @type {(el: Node) => el is HTMLElement} */ + isElement: (el) => el instanceof HTMLElement && el.className === "foo", +}; + +class C { + /** @type {(el: Node) => el is HTMLElement} */ + isElement = (el) => el instanceof HTMLElement && el.className === "foo"; +} + +// Direct assignment with @type tag on the assignment expression +let func8a; +/** @type {(el: Node) => el is HTMLElement} */ +func8a = (el) => el instanceof HTMLElement && el.className === "foo"; + +// Tag on variable declaration, assignment separately: reports error matching TS 5/6 +/** @type {(el: Node) => el is HTMLElement} */ +let func8b; +func8b = (el) => el instanceof HTMLElement && el.className === "foo"; + +/** @type {(el: Node) => el is HTMLElement} */ +module.exports = (el) => el instanceof HTMLElement && el.className === "foo"; + +/** @typedef {(el: Node) => el is HTMLElement} MyPredicate */ +/** @type {MyPredicate} */ +const func10 = (el) => el instanceof HTMLElement && el.className === "foo"; + +/** @type {(el: Node) => asserts el is HTMLElement} */ +const func11 = (el) => { + if (!(el instanceof HTMLElement)) throw new Error(); +}; + +// Parameter name mismatch: should report an error because parameter 'element' does not match 'el' +/** @type {(element: Node) => element is HTMLElement} */ +const mismatch = (el) => el instanceof HTMLElement && el.className === "foo"; + +// Non-boolean return: should report an error because number is not assignable to boolean +/** @type {(el: Node) => el is HTMLElement} */ +const badReturn = (el) => 123; From 499efd3a8e6ee35953000b6c968e139084aa79cf Mon Sep 17 00:00:00 2001 From: 1abdala Date: Tue, 24 Nov 2026 08:57:36 +0800 Subject: [PATCH 2/2] Fix JSDoc type predicate inference for arrow functions --- tsc/internal/checker/checker.go | 9 ++-- tsc/internal/checker/relater.go | 45 +++++++++++++++++++ ...peTagFunctionTypePredicateArrow.errors.txt | 43 +++--------------- ...docTypeTagFunctionTypePredicateArrow.types | 20 ++++----- 4 files changed, 68 insertions(+), 49 deletions(-) diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index f6bbdf11d8334..8c3b3c4e4171e 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -20573,7 +20573,10 @@ func (c *Checker) getSignaturesOfSymbol(symbol *ast.Symbol) []*Signature { } // If this is a function or method declaration, get the signature from the @type tag for the sake of optional parameters. // Exclude contextually-typed kinds because we already apply the @type tag to the context, plus applying it here to the initializer would suppress checks that the two are compatible. - sig := c.getSignatureOfFullSignatureType(decl) + var sig *Signature + if !ast.IsFunctionExpressionOrArrowFunction(decl) && !ast.IsObjectLiteralMethod(decl) { + sig = c.getSignatureOfFullSignatureType(decl) + } if sig == nil { sig = c.getSignatureFromDeclaration(decl) } @@ -20819,8 +20822,8 @@ func (c *Checker) getReturnTypeFromAnnotation(declaration *ast.Node) *Type { } func (c *Checker) getSignatureOfFullSignatureType(node *ast.Node) *Signature { - if ast.IsInJSFile(node) && (ast.IsFunctionDeclaration(node) || ast.IsMethodDeclaration(node) || ast.IsFunctionExpressionOrArrowFunction(node)) && node.FunctionLikeData().FullSignature != nil { - return c.getSingleCallSignature(c.getTypeFromTypeNode(node.FunctionLikeData().FullSignature)) + if tagNode := c.getTypeTagTypeNode(node); tagNode != nil { + return c.getSingleCallSignature(c.getTypeFromTypeNode(tagNode)) } return nil } diff --git a/tsc/internal/checker/relater.go b/tsc/internal/checker/relater.go index ccd1e92e8140d..67af9336dfcf7 100644 --- a/tsc/internal/checker/relater.go +++ b/tsc/internal/checker/relater.go @@ -2052,6 +2052,35 @@ func (c *Checker) getTupleElementLabelFromBindingElement(node *ast.Node, index i return "arg_" + strconv.Itoa(index) } +func (c *Checker) getTypeTagTypeNode(decl *ast.Node) *ast.Node { + if !ast.IsInJSFile(decl) || !ast.IsFunctionLike(decl) { + return nil + } + if data := decl.FunctionLikeData(); data != nil && data.FullSignature != nil { + return data.FullSignature + } + parent := decl.Parent + if parent == nil { + return nil + } + switch parent.Kind { + case ast.KindVariableDeclaration, ast.KindPropertyAssignment, ast.KindPropertyDeclaration: + if parent.Initializer() == decl { + return parent.Type() + } + case ast.KindBinaryExpression: + bin := parent.AsBinaryExpression() + if bin.Right == decl && ast.GetAssignmentDeclarationKind(parent) != ast.JSDeclarationKindNone { + return parent.Type() + } + case ast.KindExportAssignment: + if parent.Expression() == decl { + return parent.Type() + } + } + return nil +} + func (c *Checker) getTypePredicateOfSignature(sig *Signature) *TypePredicate { if sig.getResolvedTypePredicate(c) == nil { switch { @@ -2065,11 +2094,27 @@ func (c *Checker) getTypePredicateOfSignature(sig *Signature) *TypePredicate { default: if sig.declaration != nil { typeNode := sig.declaration.Type() + var jsdocPredicate *TypePredicate + if typeNode == nil && ast.IsInJSFile(sig.declaration) { + if tagNode := c.getTypeTagTypeNode(sig.declaration); tagNode != nil { + if tagNode.Kind == ast.KindFunctionType { + if retType := tagNode.AsFunctionTypeNode().Type; retType != nil && ast.IsTypePredicateNode(retType) { + typeNode = retType + } + } else { + if jsdocSig := c.getSingleCallSignature(c.getTypeFromTypeNode(tagNode)); jsdocSig != nil && jsdocSig != sig { + jsdocPredicate = c.getTypePredicateOfSignature(jsdocSig) + } + } + } + } switch { case typeNode != nil: if ast.IsTypePredicateNode(typeNode) { sig.setResolvedTypePredicate(c, c.createTypePredicateFromTypePredicateNode(typeNode, sig)) } + case jsdocPredicate != nil: + sig.setResolvedTypePredicate(c, jsdocPredicate) case ast.IsFunctionLikeDeclaration(sig.declaration) && (sig.resolvedReturnType == nil || sig.resolvedReturnType.flags&TypeFlagsBoolean != 0) && c.getParameterCount(sig) > 0: sig.setResolvedTypePredicate(c, c.noTypePredicate) // avoid infinite loop sig.setResolvedTypePredicate(c, c.getTypePredicateFromBody(sig.declaration)) diff --git a/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.errors.txt b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.errors.txt index bff92e5cde17b..542071d5395d0 100644 --- a/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.errors.txt @@ -1,28 +1,14 @@ -index.js(2,7): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. - Signature '(el: Node): boolean' must be a type predicate. -index.js(19,7): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. - Signature '(el: Node): boolean' must be a type predicate. -index.js(25,5): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. - Signature '(el: Node): boolean' must be a type predicate. -index.js(30,5): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. - Signature '(el: Node): boolean' must be a type predicate. index.js(41,1): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. Signature '(el: Node): boolean' must be a type predicate. -index.js(44,1): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. - Signature '(el: Node): boolean' must be a type predicate. -index.js(48,7): error TS2322: Type '(el: Node) => boolean' is not assignable to type 'MyPredicate'. - Signature '(el: Node): boolean' must be a type predicate. -index.js(57,7): error TS2322: Type '(el: Node) => boolean' is not assignable to type '(element: Node) => element is HTMLElement'. - Signature '(el: Node): boolean' must be a type predicate. +index.js(57,7): error TS2322: Type '(el: Node) => element is HTMLElement' is not assignable to type '(element: Node) => element is HTMLElement'. + Type predicate 'element is HTMLElement' is not assignable to 'element is HTMLElement'. + Parameter 'element' is not in the same position as parameter 'element'. index.js(61,27): error TS2322: Type 'number' is not assignable to type 'boolean'. -==== index.js (9 errors) ==== +==== index.js (3 errors) ==== /** @type {(el: Node) => el is HTMLElement} */ const func1 = (el) => el instanceof HTMLElement && el.className === "foo"; - ~~~~~ -!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. -!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. /** @type {(el: Node) => el is HTMLElement} */ const func2 = (el) => el instanceof HTMLElement; @@ -40,26 +26,17 @@ index.js(61,27): error TS2322: Type 'number' is not assignable to type 'boolean' /** @type {(el: Node) => el is HTMLElement} */ const func5 = function(el) { - ~~~~~ -!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. -!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. return el instanceof HTMLElement && el.className === "foo"; }; const obj = { /** @type {(el: Node) => el is HTMLElement} */ isElement: (el) => el instanceof HTMLElement && el.className === "foo", - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. -!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. }; class C { /** @type {(el: Node) => el is HTMLElement} */ isElement = (el) => el instanceof HTMLElement && el.className === "foo"; - ~~~~~~~~~ -!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. -!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. } // Direct assignment with @type tag on the assignment expression @@ -77,16 +54,10 @@ index.js(61,27): error TS2322: Type 'number' is not assignable to type 'boolean' /** @type {(el: Node) => el is HTMLElement} */ module.exports = (el) => el instanceof HTMLElement && el.className === "foo"; - ~~~~~~~~~~~~~~ -!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(el: Node) => el is HTMLElement'. -!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. /** @typedef {(el: Node) => el is HTMLElement} MyPredicate */ /** @type {MyPredicate} */ const func10 = (el) => el instanceof HTMLElement && el.className === "foo"; - ~~~~~~ -!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type 'MyPredicate'. -!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. /** @type {(el: Node) => asserts el is HTMLElement} */ const func11 = (el) => { @@ -97,13 +68,13 @@ index.js(61,27): error TS2322: Type 'number' is not assignable to type 'boolean' /** @type {(element: Node) => element is HTMLElement} */ const mismatch = (el) => el instanceof HTMLElement && el.className === "foo"; ~~~~~~~~ -!!! error TS2322: Type '(el: Node) => boolean' is not assignable to type '(element: Node) => element is HTMLElement'. -!!! error TS2322: Signature '(el: Node): boolean' must be a type predicate. +!!! error TS2322: Type '(el: Node) => element is HTMLElement' is not assignable to type '(element: Node) => element is HTMLElement'. +!!! error TS2322: Type predicate 'element is HTMLElement' is not assignable to 'element is HTMLElement'. +!!! error TS2322: Parameter 'element' is not in the same position as parameter 'element'. // Non-boolean return: should report an error because number is not assignable to boolean /** @type {(el: Node) => el is HTMLElement} */ const badReturn = (el) => 123; ~~~ !!! error TS2322: Type 'number' is not assignable to type 'boolean'. -!!! related TS6502 index.js:60:12: The expected type comes from the return type of this signature. \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.types b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.types index 5aed7227f9fc3..40a39bc93eb63 100644 --- a/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.types +++ b/tsc/testdata/baselines/reference/compiler/jsdocTypeTagFunctionTypePredicateArrow.types @@ -4,7 +4,7 @@ /** @type {(el: Node) => el is HTMLElement} */ const func1 = (el) => el instanceof HTMLElement && el.className === "foo"; >func1 : (el: Node) => el is HTMLElement ->(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement >el : Node >el instanceof HTMLElement && el.className === "foo" : boolean >el instanceof HTMLElement : boolean @@ -63,7 +63,7 @@ function func4(el) { /** @type {(el: Node) => el is HTMLElement} */ const func5 = function(el) { >func5 : (el: Node) => el is HTMLElement ->function(el) { return el instanceof HTMLElement && el.className === "foo";} : (el: Node) => boolean +>function(el) { return el instanceof HTMLElement && el.className === "foo";} : (el: Node) => el is HTMLElement >el : Node return el instanceof HTMLElement && el.className === "foo"; @@ -86,7 +86,7 @@ const obj = { /** @type {(el: Node) => el is HTMLElement} */ isElement: (el) => el instanceof HTMLElement && el.className === "foo", >isElement : (el: Node) => el is HTMLElement ->(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement >el : Node >el instanceof HTMLElement && el.className === "foo" : boolean >el instanceof HTMLElement : boolean @@ -106,7 +106,7 @@ class C { /** @type {(el: Node) => el is HTMLElement} */ isElement = (el) => el instanceof HTMLElement && el.className === "foo"; >isElement : (el: Node) => el is HTMLElement ->(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement >el : Node >el instanceof HTMLElement && el.className === "foo" : boolean >el instanceof HTMLElement : boolean @@ -161,11 +161,11 @@ func8b = (el) => el instanceof HTMLElement && el.className === "foo"; /** @type {(el: Node) => el is HTMLElement} */ module.exports = (el) => el instanceof HTMLElement && el.className === "foo"; ->module.exports = (el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>module.exports = (el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement >module.exports : (el: Node) => el is HTMLElement >module : { exports: (el: Node) => el is HTMLElement; } >exports : (el: Node) => el is HTMLElement ->(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement >el : Node >el instanceof HTMLElement && el.className === "foo" : boolean >el instanceof HTMLElement : boolean @@ -181,7 +181,7 @@ module.exports = (el) => el instanceof HTMLElement && el.className === "foo"; /** @type {MyPredicate} */ const func10 = (el) => el instanceof HTMLElement && el.className === "foo"; >func10 : MyPredicate ->(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => el is HTMLElement >el : Node >el instanceof HTMLElement && el.className === "foo" : boolean >el instanceof HTMLElement : boolean @@ -196,7 +196,7 @@ const func10 = (el) => el instanceof HTMLElement && el.className === "foo"; /** @type {(el: Node) => asserts el is HTMLElement} */ const func11 = (el) => { >func11 : (el: Node) => asserts el is HTMLElement ->(el) => { if (!(el instanceof HTMLElement)) throw new Error();} : (el: Node) => void +>(el) => { if (!(el instanceof HTMLElement)) throw new Error();} : (el: Node) => asserts el is HTMLElement >el : Node if (!(el instanceof HTMLElement)) throw new Error(); @@ -214,7 +214,7 @@ const func11 = (el) => { /** @type {(element: Node) => element is HTMLElement} */ const mismatch = (el) => el instanceof HTMLElement && el.className === "foo"; >mismatch : (element: Node) => element is HTMLElement ->(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => boolean +>(el) => el instanceof HTMLElement && el.className === "foo" : (el: Node) => element is HTMLElement >el : Node >el instanceof HTMLElement && el.className === "foo" : boolean >el instanceof HTMLElement : boolean @@ -230,7 +230,7 @@ const mismatch = (el) => el instanceof HTMLElement && el.className === "foo"; /** @type {(el: Node) => el is HTMLElement} */ const badReturn = (el) => 123; >badReturn : (el: Node) => el is HTMLElement ->(el) => 123 : (el: Node) => number +>(el) => 123 : (el: Node) => el is HTMLElement >el : Node >123 : 123