Skip to content

Report assignability errors on the satisfies keyword #53797

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

Merged
Merged
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
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34571,7 +34571,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (isErrorType(targetType)) {
return targetType;
}
checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, target, expression, Diagnostics.Type_0_does_not_satisfy_the_expected_type_1);
const errorNode = findAncestor(target.parent, n => n.kind === SyntaxKind.SatisfiesExpression || n.kind === SyntaxKind.JSDocSatisfiesTag);
checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, errorNode, expression, Diagnostics.Type_0_does_not_satisfy_the_expected_type_1);
return exprType;
}

Expand Down
9 changes: 9 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ import {
JSDocParameterTag,
JSDocPropertyLikeTag,
JSDocSatisfiesExpression,
JSDocSatisfiesTag,
JSDocSignature,
JSDocTag,
JSDocTemplateTag,
Expand Down Expand Up @@ -2231,6 +2232,14 @@ export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpa
const pos = skipTrivia(sourceFile.text, (node as ReturnStatement | YieldExpression).pos);
return getSpanOfTokenAtPosition(sourceFile, pos);
}
case SyntaxKind.SatisfiesExpression: {
const pos = skipTrivia(sourceFile.text, (node as SatisfiesExpression).expression.end);
return getSpanOfTokenAtPosition(sourceFile, pos);
}
case SyntaxKind.JSDocSatisfiesTag: {
const pos = skipTrivia(sourceFile.text, (node as JSDocSatisfiesTag).tagName.pos);
return getSpanOfTokenAtPosition(sourceFile, pos);
}
}

if (errorNode === undefined) {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocSatisfiesTag1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/a.js(21,44): error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
/a.js(22,28): error TS1360: Type '{}' does not satisfy the expected type 'T1'.
/a.js(22,17): error TS1360: Type '{}' does not satisfy the expected type 'T1'.
Property 'a' is missing in type '{}' but required in type 'T1'.
/a.js(31,49): error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T4'.
Object literal may only specify known properties, and 'b' does not exist in type 'T4'.
Expand Down Expand Up @@ -32,7 +32,7 @@
!!! error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
const t3 = /** @satisfies {T1} */ ({});
~~
~~~~~~~~~
!!! error TS1360: Type '{}' does not satisfy the expected type 'T1'.
!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'T1'.
!!! related TS2728 /a.js:3:4: 'a' is declared here.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocSatisfiesTag12.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
/a.js(44,25): error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T2'.
Object literal may only specify known properties, and 'b' does not exist in type 'T2'.
/a.js(51,17): error TS1360: Type 'number' does not satisfy the expected type 'string'.
/a.js(51,6): error TS1360: Type 'number' does not satisfy the expected type 'string'.


==== /a.js (3 errors) ====
Expand Down Expand Up @@ -63,6 +63,6 @@
const t7 = { a: "a" };

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

4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocSatisfiesTag4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/a.js(5,32): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
/a.js(5,21): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
Property 'a' is missing in type '{}' but required in type 'Foo'.


Expand All @@ -8,7 +8,7 @@
* @property {number} a
*/
export default /** @satisfies {Foo} */ ({});
~~~
~~~~~~~~~
!!! error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'Foo'.
!!! related TS2728 /a.js:3:4: 'a' is declared here.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/typeSatisfaction.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(12,20): error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'I1'.
Object literal may only specify known properties, and 'b' does not exist in type 'I1'.
tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(13,26): error TS1360: Type '{}' does not satisfy the expected type 'I1'.
tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(13,16): error TS1360: Type '{}' does not satisfy the expected type 'I1'.
Property 'a' is missing in type '{}' but required in type 'I1'.
tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(24,23): error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'A'.
Object literal may only specify known properties, and 'b' does not exist in type 'A'.
Expand All @@ -23,7 +23,7 @@ tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(24,23):
!!! error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'I1'.
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'I1'.
const t3 = { } satisfies I1; // Error
~~
~~~~~~~~~
!!! error TS1360: Type '{}' does not satisfy the expected type 'I1'.
!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'I1'.
!!! related TS2728 tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts:2:5: 'a' is declared here.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/expressions/typeSatisfaction/a.ts(4,29): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
tests/cases/conformance/expressions/typeSatisfaction/a.ts(4,19): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
Property 'a' is missing in type '{}' but required in type 'Foo'.


Expand All @@ -7,7 +7,7 @@ tests/cases/conformance/expressions/typeSatisfaction/a.ts(4,29): error TS1360: T
a: number;
}
export default {} satisfies Foo;
~~~
~~~~~~~~~
!!! error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'Foo'.
!!! related TS2728 tests/cases/conformance/expressions/typeSatisfaction/a.ts:2:5: 'a' is declared here.
Expand Down