Skip to content

Fixed types of parenthesized expressions at locations with @satisfies and @type mix #61852

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4588,13 +4588,13 @@ export function getJSDocCommentsAndTags(hostNode: Node, noCache?: boolean): read
let result: (JSDoc | JSDocTag)[] | undefined;
// Pull parameter comments from declaring function as well
if (isVariableLike(hostNode) && hasInitializer(hostNode) && hasJSDocNodes(hostNode.initializer!)) {
result = addRange(result, filterOwnedJSDocTags(hostNode, hostNode.initializer.jsDoc!));
result = addRange(result, filterOwnedJSDocTags(hostNode, hostNode.initializer.jsDoc!, result));
}

let node: Node | undefined = hostNode;
while (node && node.parent) {
if (hasJSDocNodes(node)) {
result = addRange(result, filterOwnedJSDocTags(hostNode, node.jsDoc!));
result = addRange(result, filterOwnedJSDocTags(hostNode, node.jsDoc!, result));
}

if (node.kind === SyntaxKind.Parameter) {
Expand All @@ -4610,11 +4610,11 @@ export function getJSDocCommentsAndTags(hostNode: Node, noCache?: boolean): read
return result || emptyArray;
}

function filterOwnedJSDocTags(hostNode: Node, comments: JSDocArray) {
function filterOwnedJSDocTags(hostNode: Node, comments: JSDocArray, result: (JSDoc | JSDocTag)[] | undefined) {
const lastJsDoc = last(comments);
return flatMap<JSDoc, JSDoc | JSDocTag>(comments, jsDoc => {
if (jsDoc === lastJsDoc) {
const ownedTags = filter(jsDoc.tags, tag => ownsJSDocTag(hostNode, tag));
const ownedTags = filter(jsDoc.tags, tag => ownsJSDocTag(hostNode, tag, result));
return jsDoc.tags === ownedTags ? [jsDoc] : ownedTags;
}
else {
Expand All @@ -4627,14 +4627,24 @@ function filterOwnedJSDocTags(hostNode: Node, comments: JSDocArray) {
* Determines whether a host node owns a jsDoc tag. A `@type`/`@satisfies` tag attached to a
* a ParenthesizedExpression belongs only to the ParenthesizedExpression.
*/
function ownsJSDocTag(hostNode: Node, tag: JSDocTag) {
return !(isJSDocTypeTag(tag) || isJSDocSatisfiesTag(tag))
|| !tag.parent
function ownsJSDocTag(hostNode: Node, tag: JSDocTag, result: (JSDoc | JSDocTag)[] | undefined) {
if (
(isJSDocTypeOrSatisfiesTag(tag)) && some(result, t => {
return isJSDoc(t) ? some(t.tags, isJSDocTypeOrSatisfiesTag) : isJSDocTypeOrSatisfiesTag(t);
})
) {
return false;
}
Comment on lines +4631 to +4637
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rationale is that in TS files a single node can't own both at the same time

return !tag.parent
|| !isJSDoc(tag.parent)
|| !isParenthesizedExpression(tag.parent.parent)
|| tag.parent.parent === hostNode;
}

function isJSDocTypeOrSatisfiesTag(tag: JSDocTag) {
return isJSDocTypeTag(tag) || isJSDocSatisfiesTag(tag);
}

/** @internal */
export function getNextJSDocCommentLocation(node: Node): Node | undefined {
const parent = node.parent;
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/checkJsdocSatisfiesTag1.types
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ const t3 = /** @satisfies {T1} */ ({});
const t4 = /** @satisfies {T2} */ ({ a: "a" });
>t4 : T2
> : ^^
>({ a: "a" }) : T2
> : ^^
>({ a: "a" }) : { a: "a"; }
> : ^^^^^^^^^^^
>{ a: "a" } : { a: "a"; }
> : ^^^^^^^^^^^
>a : "a"
Expand All @@ -74,7 +74,7 @@ const t5 = /** @satisfies {T3} */((m) => m.substring(0));
>t5 : (m: string) => string
> : ^ ^^ ^^^^^
>((m) => m.substring(0)) : (m: string) => string
> : ^ ^^ ^^^^^
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of interesting, given the old one showed reuse here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes some sense - this is now the function expression's type that is contextually-typed by the @satisfies tag and not the annotation type coming from @type. @type's content can be reused (I guess) but the expression's type is "unique" so its parts can't be reused as easily

> : ^ ^^^^^^^^^^^^^^^^^^^
>(m) => m.substring(0) : (m: string) => string
> : ^ ^^^^^^^^^^^^^^^^^^^
>m : string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/a.js(13,5): error TS1223: 'satisfies' tag already specified.
/a.js(18,5): error TS1223: 'satisfies' tag already specified.


==== /a.js (2 errors) ====
==== /a.js (1 errors) ====
/**
* @typedef {Object} T1
* @property {number} a
Expand All @@ -23,8 +22,6 @@

/**
* @satisfies {number}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as shown by the new tests/cases/conformance/jsdoc/checkJsdocSatisfiesTag16.ts both are still checked - but they are no longer treated as duplicates. I think this is fine but maybe @sandersn would have a different opinion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's correct to drop the error, but the first tag shouldn't even be checked--the comment on ownsJSDocTag says that satisfies tags not on parentheses don't apply to anything. I think the real bug is that type tags shouldn't apply to the parenthesized expression in this case either, but fails because @type usage is ambiguous.

~~~~~~~~~
!!! error TS1223: 'satisfies' tag already specified.
*/
const t2 = /** @satisfies {number} */ (1);

35 changes: 35 additions & 0 deletions tests/baselines/reference/checkJsdocSatisfiesTag16.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/a.js(9,17): error TS1360: Type 'number' does not satisfy the expected type 'string'.
/a.js(12,5): error TS1360: Type 'number' does not satisfy the expected type 'string'.
/a.js(17,5): error TS1360: Type 'number' does not satisfy the expected type 'string'.
/a.js(19,17): error TS1360: Type 'number' does not satisfy the expected type 'string'.


==== /a.js (4 errors) ====
/**
* @satisfies {number}
*/
const t1 = /** @satisfies {number} */ (1);

/**
* @satisfies {number}
*/
const t2 = /** @satisfies {string} */ (1);
~~~~~~~~~
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'.

/**
* @satisfies {string}
~~~~~~~~~
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'.
*/
const t3 = /** @satisfies {number} */ (1);

/**
* @satisfies {string}
~~~~~~~~~
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'.
*/
const t4 = /** @satisfies {string} */ (1);
~~~~~~~~~
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'.

27 changes: 27 additions & 0 deletions tests/baselines/reference/checkJsdocSatisfiesTag16.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/conformance/jsdoc/checkJsdocSatisfiesTag16.ts] ////

=== /a.js ===
/**
* @satisfies {number}
*/
const t1 = /** @satisfies {number} */ (1);
>t1 : Symbol(t1, Decl(a.js, 3, 5))

/**
* @satisfies {number}
*/
const t2 = /** @satisfies {string} */ (1);
>t2 : Symbol(t2, Decl(a.js, 8, 5))

/**
* @satisfies {string}
*/
const t3 = /** @satisfies {number} */ (1);
>t3 : Symbol(t3, Decl(a.js, 13, 5))

/**
* @satisfies {string}
*/
const t4 = /** @satisfies {string} */ (1);
>t4 : Symbol(t4, Decl(a.js, 18, 5))

47 changes: 47 additions & 0 deletions tests/baselines/reference/checkJsdocSatisfiesTag16.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [tests/cases/conformance/jsdoc/checkJsdocSatisfiesTag16.ts] ////

=== /a.js ===
/**
* @satisfies {number}
*/
const t1 = /** @satisfies {number} */ (1);
>t1 : 1
> : ^
>(1) : 1
> : ^
>1 : 1
> : ^

/**
* @satisfies {number}
*/
const t2 = /** @satisfies {string} */ (1);
>t2 : 1
> : ^
>(1) : 1
> : ^
>1 : 1
> : ^

/**
* @satisfies {string}
*/
const t3 = /** @satisfies {number} */ (1);
>t3 : 1
> : ^
>(1) : 1
> : ^
>1 : 1
> : ^

/**
* @satisfies {string}
*/
const t4 = /** @satisfies {string} */ (1);
>t4 : 1
> : ^
>(1) : 1
> : ^
>1 : 1
> : ^

4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocSatisfiesTag3.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
let obj = /** @satisfies {{ g(s: string): void } & Record<string, unknown>} */ ({
>obj : { f(s: string): void; } & Record<string, unknown>
> : ^^^^ ^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>({ f(s) { }, // "incorrect" implicit any on 's' g(s) { }}) : { f(s: string): void; } & Record<string, unknown>
> : ^^^^ ^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>({ f(s) { }, // "incorrect" implicit any on 's' g(s) { }}) : { f(s: any): void; g(s: string): void; }
Copy link
Contributor Author

@Andarist Andarist Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we can see here - type of this parenthesized expression is now the same as the inner's expression, which I think is the actual desired outcome (and the actual result as implemented by microsoft/typescript-go#1157 )

> : ^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
>{ f(s) { }, // "incorrect" implicit any on 's' g(s) { }} : { f(s: any): void; g(s: string): void; }
> : ^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^

Expand Down
26 changes: 26 additions & 0 deletions tests/cases/conformance/jsdoc/checkJsdocSatisfiesTag16.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @strict: true
// @allowJS: true
// @checkJs: true
// @noEmit: true

// @filename: /a.js

/**
* @satisfies {number}
*/
const t1 = /** @satisfies {number} */ (1);

/**
* @satisfies {number}
*/
const t2 = /** @satisfies {string} */ (1);

/**
* @satisfies {string}
*/
const t3 = /** @satisfies {number} */ (1);

/**
* @satisfies {string}
*/
const t4 = /** @satisfies {string} */ (1);