Skip to content

Commit 5897d7a

Browse files
authored
Report assignability errors on the satisfies keyword (#53797)
1 parent bdcf8ab commit 5897d7a

7 files changed

+21
-11
lines changed

src/compiler/checker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -34618,7 +34618,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3461834618
if (isErrorType(targetType)) {
3461934619
return targetType;
3462034620
}
34621-
checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, target, expression, Diagnostics.Type_0_does_not_satisfy_the_expected_type_1);
34621+
const errorNode = findAncestor(target.parent, n => n.kind === SyntaxKind.SatisfiesExpression || n.kind === SyntaxKind.JSDocSatisfiesTag);
34622+
checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, errorNode, expression, Diagnostics.Type_0_does_not_satisfy_the_expected_type_1);
3462234623
return exprType;
3462334624
}
3462434625

src/compiler/utilities.ts

+9
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ import {
350350
JSDocParameterTag,
351351
JSDocPropertyLikeTag,
352352
JSDocSatisfiesExpression,
353+
JSDocSatisfiesTag,
353354
JSDocSignature,
354355
JSDocTag,
355356
JSDocTemplateTag,
@@ -2234,6 +2235,14 @@ export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpa
22342235
const pos = skipTrivia(sourceFile.text, (node as ReturnStatement | YieldExpression).pos);
22352236
return getSpanOfTokenAtPosition(sourceFile, pos);
22362237
}
2238+
case SyntaxKind.SatisfiesExpression: {
2239+
const pos = skipTrivia(sourceFile.text, (node as SatisfiesExpression).expression.end);
2240+
return getSpanOfTokenAtPosition(sourceFile, pos);
2241+
}
2242+
case SyntaxKind.JSDocSatisfiesTag: {
2243+
const pos = skipTrivia(sourceFile.text, (node as JSDocSatisfiesTag).tagName.pos);
2244+
return getSpanOfTokenAtPosition(sourceFile, pos);
2245+
}
22372246
}
22382247

22392248
if (errorNode === undefined) {

tests/baselines/reference/checkJsdocSatisfiesTag1.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/a.js(21,44): error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
22
Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
3-
/a.js(22,28): error TS1360: Type '{}' does not satisfy the expected type 'T1'.
3+
/a.js(22,17): error TS1360: Type '{}' does not satisfy the expected type 'T1'.
44
Property 'a' is missing in type '{}' but required in type 'T1'.
55
/a.js(31,49): error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T4'.
66
Object literal may only specify known properties, and 'b' does not exist in type 'T4'.
@@ -32,7 +32,7 @@
3232
!!! error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
3333
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
3434
const t3 = /** @satisfies {T1} */ ({});
35-
~~
35+
~~~~~~~~~
3636
!!! error TS1360: Type '{}' does not satisfy the expected type 'T1'.
3737
!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'T1'.
3838
!!! related TS2728 /a.js:3:4: 'a' is declared here.

tests/baselines/reference/checkJsdocSatisfiesTag12.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
33
/a.js(44,25): error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T2'.
44
Object literal may only specify known properties, and 'b' does not exist in type 'T2'.
5-
/a.js(51,17): error TS1360: Type 'number' does not satisfy the expected type 'string'.
5+
/a.js(51,6): error TS1360: Type 'number' does not satisfy the expected type 'string'.
66

77

88
==== /a.js (3 errors) ====
@@ -63,6 +63,6 @@
6363
const t7 = { a: "a" };
6464

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

tests/baselines/reference/checkJsdocSatisfiesTag4.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/a.js(5,32): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
1+
/a.js(5,21): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
22
Property 'a' is missing in type '{}' but required in type 'Foo'.
33

44

@@ -8,7 +8,7 @@
88
* @property {number} a
99
*/
1010
export default /** @satisfies {Foo} */ ({});
11-
~~~
11+
~~~~~~~~~
1212
!!! error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
1313
!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'Foo'.
1414
!!! related TS2728 /a.js:3:4: 'a' is declared here.

tests/baselines/reference/typeSatisfaction.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(12,20): error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'I1'.
22
Object literal may only specify known properties, and 'b' does not exist in type 'I1'.
3-
tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(13,26): error TS1360: Type '{}' does not satisfy the expected type 'I1'.
3+
tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(13,16): error TS1360: Type '{}' does not satisfy the expected type 'I1'.
44
Property 'a' is missing in type '{}' but required in type 'I1'.
55
tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(24,23): error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'A'.
66
Object literal may only specify known properties, and 'b' does not exist in type 'A'.
@@ -23,7 +23,7 @@ tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts(24,23):
2323
!!! error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'I1'.
2424
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'I1'.
2525
const t3 = { } satisfies I1; // Error
26-
~~
26+
~~~~~~~~~
2727
!!! error TS1360: Type '{}' does not satisfy the expected type 'I1'.
2828
!!! error TS1360: Property 'a' is missing in type '{}' but required in type 'I1'.
2929
!!! related TS2728 tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction.ts:2:5: 'a' is declared here.

tests/baselines/reference/typeSatisfactionWithDefaultExport.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/expressions/typeSatisfaction/a.ts(4,29): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
1+
tests/cases/conformance/expressions/typeSatisfaction/a.ts(4,19): error TS1360: Type '{}' does not satisfy the expected type 'Foo'.
22
Property 'a' is missing in type '{}' but required in type 'Foo'.
33

44

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

0 commit comments

Comments
 (0)