Skip to content

Commit cc37e4f

Browse files
committed
feat(7481): add explicit type compatibility check with 'satisfies' expression
1 parent d417058 commit cc37e4f

38 files changed

+1231
-439
lines changed

Diff for: src/compiler/checker.ts

+18
Original file line numberDiff line numberDiff line change
@@ -26544,6 +26544,8 @@ namespace ts {
2654426544
}
2654526545
case SyntaxKind.NonNullExpression:
2654626546
return getContextualType(parent as NonNullExpression, contextFlags);
26547+
case SyntaxKind.SatisfiesExpression:
26548+
return getTypeFromTypeNode((parent as SatisfiesExpression).type);
2654726549
case SyntaxKind.JsxExpression:
2654826550
return getContextualTypeForJsxExpression(parent as JsxExpression);
2654926551
case SyntaxKind.JsxAttribute:
@@ -31348,6 +31350,20 @@ namespace ts {
3134831350
getNonNullableType(checkExpression(node.expression));
3134931351
}
3135031352

31353+
function checkSatisfiesExpression(node: SatisfiesExpression) {
31354+
checkSourceElement(node.type);
31355+
31356+
const targetType = getTypeFromTypeNode(node.type);
31357+
if (isErrorType(targetType)) {
31358+
return targetType;
31359+
}
31360+
31361+
const exprType = checkExpression(node.expression);
31362+
checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, node.type, node.expression, Diagnostics.Type_0_does_not_satisfy_the_expected_type_1);
31363+
31364+
return exprType;
31365+
}
31366+
3135131367
function checkMetaProperty(node: MetaProperty): Type {
3135231368
checkGrammarMetaProperty(node);
3135331369

@@ -34100,6 +34116,8 @@ namespace ts {
3410034116
return checkAssertion(node as AssertionExpression);
3410134117
case SyntaxKind.NonNullExpression:
3410234118
return checkNonNullAssertion(node as NonNullExpression);
34119+
case SyntaxKind.SatisfiesExpression:
34120+
return checkSatisfiesExpression(node as SatisfiesExpression);
3410334121
case SyntaxKind.MetaProperty:
3410434122
return checkMetaProperty(node as MetaProperty);
3410534123
case SyntaxKind.DeleteExpression:

Diff for: src/compiler/diagnosticMessages.json

+8
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@
10601060
"category": "Error",
10611061
"code": 1359
10621062
},
1063+
"Type '{0}' does not satisfy the expected type '{1}'.": {
1064+
"category": "Error",
1065+
"code": 1360
1066+
},
10631067
"'{0}' cannot be used as a value because it was imported using 'import type'.": {
10641068
"category": "Error",
10651069
"code": 1361
@@ -6173,6 +6177,10 @@
61736177
"category": "Error",
61746178
"code": 8034
61756179
},
6180+
"Type satisfaction expressions can only be used in TypeScript files.": {
6181+
"category": "Error",
6182+
"code": 8035
6183+
},
61766184
"Declaration emit for this file requires using private name '{0}'. An explicit type annotation may unblock declaration emit.": {
61776185
"category": "Error",
61786186
"code": 9005

Diff for: src/compiler/emitter.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,8 @@ namespace ts {
17271727
return emitAsExpression(node as AsExpression);
17281728
case SyntaxKind.NonNullExpression:
17291729
return emitNonNullExpression(node as NonNullExpression);
1730+
case SyntaxKind.SatisfiesExpression:
1731+
return emitSatisfiesExpression(node as SatisfiesExpression);
17301732
case SyntaxKind.MetaProperty:
17311733
return emitMetaProperty(node as MetaProperty);
17321734
case SyntaxKind.SyntheticExpression:
@@ -2799,6 +2801,16 @@ namespace ts {
27992801
writeOperator("!");
28002802
}
28012803

2804+
function emitSatisfiesExpression(node: SatisfiesExpression) {
2805+
emitExpression(node.expression, /*parenthesizerRules*/ undefined);
2806+
if (node.type) {
2807+
writeSpace();
2808+
writeKeyword("satisfies");
2809+
writeSpace();
2810+
emit(node.type);
2811+
}
2812+
}
2813+
28022814
function emitMetaProperty(node: MetaProperty) {
28032815
writeToken(node.keywordToken, node.pos, writePunctuation);
28042816
writePunctuation(".");

Diff for: src/compiler/factory/nodeFactory.ts

+24
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ namespace ts {
218218
updateAsExpression,
219219
createNonNullExpression,
220220
updateNonNullExpression,
221+
createSatisfiesExpression,
222+
updateSatisfiesExpression,
221223
createNonNullChain,
222224
updateNonNullChain,
223225
createMetaProperty,
@@ -3099,6 +3101,26 @@ namespace ts {
30993101
: node;
31003102
}
31013103

3104+
// @api
3105+
function createSatisfiesExpression(expression: Expression, type: TypeNode) {
3106+
const node = createBaseExpression<SatisfiesExpression>(SyntaxKind.SatisfiesExpression);
3107+
node.expression = expression;
3108+
node.type = type;
3109+
node.transformFlags |=
3110+
propagateChildFlags(node.expression) |
3111+
propagateChildFlags(node.type) |
3112+
TransformFlags.ContainsTypeScript;
3113+
return node;
3114+
}
3115+
3116+
// @api
3117+
function updateSatisfiesExpression(node: SatisfiesExpression, expression: Expression, type: TypeNode) {
3118+
return node.expression !== expression
3119+
|| node.type !== type
3120+
? update(createSatisfiesExpression(expression, type), node)
3121+
: node;
3122+
}
3123+
31023124
// @api
31033125
function createNonNullChain(expression: Expression) {
31043126
const node = createBaseExpression<NonNullChain>(SyntaxKind.NonNullExpression);
@@ -5590,6 +5612,7 @@ namespace ts {
55905612
case SyntaxKind.ParenthesizedExpression: return updateParenthesizedExpression(outerExpression, expression);
55915613
case SyntaxKind.TypeAssertionExpression: return updateTypeAssertion(outerExpression, outerExpression.type, expression);
55925614
case SyntaxKind.AsExpression: return updateAsExpression(outerExpression, expression, outerExpression.type);
5615+
case SyntaxKind.SatisfiesExpression: return updateSatisfiesExpression(outerExpression, expression, outerExpression.type);
55935616
case SyntaxKind.NonNullExpression: return updateNonNullExpression(outerExpression, expression);
55945617
case SyntaxKind.PartiallyEmittedExpression: return updatePartiallyEmittedExpression(outerExpression, expression);
55955618
}
@@ -6320,6 +6343,7 @@ namespace ts {
63206343
case SyntaxKind.ArrayBindingPattern:
63216344
return TransformFlags.BindingPatternExcludes;
63226345
case SyntaxKind.TypeAssertionExpression:
6346+
case SyntaxKind.SatisfiesExpression:
63236347
case SyntaxKind.AsExpression:
63246348
case SyntaxKind.PartiallyEmittedExpression:
63256349
case SyntaxKind.ParenthesizedExpression:

Diff for: src/compiler/factory/nodeTests.ts

+4
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ namespace ts {
433433
return node.kind === SyntaxKind.AsExpression;
434434
}
435435

436+
export function isSatisfiesExpression(node: Node): node is SatisfiesExpression {
437+
return node.kind === SyntaxKind.SatisfiesExpression;
438+
}
439+
436440
export function isNonNullExpression(node: Node): node is NonNullExpression {
437441
return node.kind === SyntaxKind.NonNullExpression;
438442
}

Diff for: src/compiler/factory/utilities.ts

+1
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ namespace ts {
437437
return (kinds & OuterExpressionKinds.Parentheses) !== 0;
438438
case SyntaxKind.TypeAssertionExpression:
439439
case SyntaxKind.AsExpression:
440+
case SyntaxKind.SatisfiesExpression:
440441
return (kinds & OuterExpressionKinds.TypeAssertions) !== 0;
441442
case SyntaxKind.NonNullExpression:
442443
return (kinds & OuterExpressionKinds.NonNullAssertions) !== 0;

Diff for: src/compiler/parser.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ namespace ts {
276276
visitNode(cbNode, (node as AsExpression).type);
277277
case SyntaxKind.NonNullExpression:
278278
return visitNode(cbNode, (node as NonNullExpression).expression);
279+
case SyntaxKind.SatisfiesExpression:
280+
return visitNode(cbNode, (node as SatisfiesExpression).expression) ||
281+
visitNode(cbNode, (node as SatisfiesExpression).type);
279282
case SyntaxKind.MetaProperty:
280283
return visitNode(cbNode, (node as MetaProperty).name);
281284
case SyntaxKind.ConditionalExpression:
@@ -4677,7 +4680,7 @@ namespace ts {
46774680
break;
46784681
}
46794682

4680-
if (token() === SyntaxKind.AsKeyword) {
4683+
if (token() === SyntaxKind.AsKeyword || token() === SyntaxKind.SatisfiesKeyword) {
46814684
// Make sure we *do* perform ASI for constructs like this:
46824685
// var x = foo
46834686
// as (Bar)
@@ -4687,8 +4690,10 @@ namespace ts {
46874690
break;
46884691
}
46894692
else {
4693+
const keywordKind = token();
46904694
nextToken();
4691-
leftOperand = makeAsExpression(leftOperand, parseType());
4695+
leftOperand = keywordKind === SyntaxKind.SatisfiesKeyword ? makeSatisfiesExpression(leftOperand, parseType()) :
4696+
makeAsExpression(leftOperand, parseType());
46924697
}
46934698
}
46944699
else {
@@ -4707,6 +4712,10 @@ namespace ts {
47074712
return getBinaryOperatorPrecedence(token()) > 0;
47084713
}
47094714

4715+
function makeSatisfiesExpression(left: Expression, right: TypeNode): SatisfiesExpression {
4716+
return finishNode(factory.createSatisfiesExpression(left, right), left.pos);
4717+
}
4718+
47104719
function makeBinaryExpression(left: Expression, operatorToken: BinaryOperatorToken, right: Expression, pos: number): BinaryExpression {
47114720
return finishNode(factory.createBinaryExpression(left, operatorToken, right), pos);
47124721
}

Diff for: src/compiler/program.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,9 @@ namespace ts {
22742274
case SyntaxKind.AsExpression:
22752275
diagnostics.push(createDiagnosticForNode((node as AsExpression).type, Diagnostics.Type_assertion_expressions_can_only_be_used_in_TypeScript_files));
22762276
return "skip";
2277+
case SyntaxKind.SatisfiesExpression:
2278+
diagnostics.push(createDiagnosticForNode((node as SatisfiesExpression).type, Diagnostics.Type_satisfaction_expressions_can_only_be_used_in_TypeScript_files));
2279+
return "skip";
22772280
case SyntaxKind.TypeAssertionExpression:
22782281
Debug.fail(); // Won't parse these in a JS file anyway, as they are interpreted as JSX.
22792282
}

Diff for: src/compiler/scanner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ namespace ts {
135135
require: SyntaxKind.RequireKeyword,
136136
global: SyntaxKind.GlobalKeyword,
137137
return: SyntaxKind.ReturnKeyword,
138+
satisfies: SyntaxKind.SatisfiesKeyword,
138139
set: SyntaxKind.SetKeyword,
139140
static: SyntaxKind.StaticKeyword,
140141
string: SyntaxKind.StringKeyword,

Diff for: src/compiler/transformers/ts.ts

+8
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,9 @@ namespace ts {
513513
// TypeScript type assertions are removed, but their subtrees are preserved.
514514
return visitAssertionExpression(node as AssertionExpression);
515515

516+
case SyntaxKind.SatisfiesExpression:
517+
return visitSatisfiesExpression(node as SatisfiesExpression);
518+
516519
case SyntaxKind.CallExpression:
517520
return visitCallExpression(node as CallExpression);
518521

@@ -2257,6 +2260,11 @@ namespace ts {
22572260
return factory.createPartiallyEmittedExpression(expression, node);
22582261
}
22592262

2263+
function visitSatisfiesExpression(node: SatisfiesExpression): Expression {
2264+
const expression = visitNode(node.expression, visitor, isExpression);
2265+
return factory.createPartiallyEmittedExpression(expression, node);
2266+
}
2267+
22602268
function visitCallExpression(node: CallExpression) {
22612269
return factory.updateCallExpression(
22622270
node,

Diff for: src/compiler/types.ts

+12
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ namespace ts {
180180
RequireKeyword,
181181
NumberKeyword,
182182
ObjectKeyword,
183+
SatisfiesKeyword,
183184
SetKeyword,
184185
StringKeyword,
185186
SymbolKeyword,
@@ -273,6 +274,7 @@ namespace ts {
273274
NonNullExpression,
274275
MetaProperty,
275276
SyntheticExpression,
277+
SatisfiesExpression,
276278

277279
// Misc
278280
TemplateSpan,
@@ -602,6 +604,7 @@ namespace ts {
602604
| SyntaxKind.OverrideKeyword
603605
| SyntaxKind.RequireKeyword
604606
| SyntaxKind.ReturnKeyword
607+
| SyntaxKind.SatisfiesKeyword
605608
| SyntaxKind.SetKeyword
606609
| SyntaxKind.StaticKeyword
607610
| SyntaxKind.StringKeyword
@@ -2485,6 +2488,12 @@ namespace ts {
24852488
readonly expression: UnaryExpression;
24862489
}
24872490

2491+
export interface SatisfiesExpression extends Expression {
2492+
readonly kind: SyntaxKind.SatisfiesExpression;
2493+
readonly expression: Expression;
2494+
readonly type: TypeNode;
2495+
}
2496+
24882497
export type AssertionExpression =
24892498
| TypeAssertion
24902499
| AsExpression
@@ -7037,6 +7046,7 @@ namespace ts {
70377046
export type OuterExpression =
70387047
| ParenthesizedExpression
70397048
| TypeAssertion
7049+
| SatisfiesExpression
70407050
| AsExpression
70417051
| NonNullExpression
70427052
| PartiallyEmittedExpression;
@@ -7366,6 +7376,8 @@ namespace ts {
73667376
updateNonNullChain(node: NonNullChain, expression: Expression): NonNullChain;
73677377
createMetaProperty(keywordToken: MetaProperty["keywordToken"], name: Identifier): MetaProperty;
73687378
updateMetaProperty(node: MetaProperty, name: Identifier): MetaProperty;
7379+
createSatisfiesExpression(expression: Expression, type: TypeNode): SatisfiesExpression;
7380+
updateSatisfiesExpression(node: SatisfiesExpression, expression: Expression, type: TypeNode): SatisfiesExpression;
73697381

73707382
//
73717383
// Misc

Diff for: src/compiler/utilities.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,7 @@ namespace ts {
19211921
case SyntaxKind.TaggedTemplateExpression:
19221922
case SyntaxKind.AsExpression:
19231923
case SyntaxKind.TypeAssertionExpression:
1924+
case SyntaxKind.SatisfiesExpression:
19241925
case SyntaxKind.NonNullExpression:
19251926
case SyntaxKind.ParenthesizedExpression:
19261927
case SyntaxKind.FunctionExpression:
@@ -2019,6 +2020,8 @@ namespace ts {
20192020
return (parent as ExpressionWithTypeArguments).expression === node && isExpressionWithTypeArgumentsInClassExtendsClause(parent);
20202021
case SyntaxKind.ShorthandPropertyAssignment:
20212022
return (parent as ShorthandPropertyAssignment).objectAssignmentInitializer === node;
2023+
case SyntaxKind.SatisfiesExpression:
2024+
return node === (parent as SatisfiesExpression).expression;
20222025
default:
20232026
return isExpressionNode(parent);
20242027
}
@@ -3707,6 +3710,7 @@ namespace ts {
37073710
return OperatorPrecedence.Member;
37083711

37093712
case SyntaxKind.AsExpression:
3713+
case SyntaxKind.SatisfiesExpression:
37103714
return OperatorPrecedence.Relational;
37113715

37123716
case SyntaxKind.ThisKeyword:
@@ -3765,6 +3769,7 @@ namespace ts {
37653769
case SyntaxKind.InstanceOfKeyword:
37663770
case SyntaxKind.InKeyword:
37673771
case SyntaxKind.AsKeyword:
3772+
case SyntaxKind.SatisfiesKeyword:
37683773
return OperatorPrecedence.Relational;
37693774
case SyntaxKind.LessThanLessThanToken:
37703775
case SyntaxKind.GreaterThanGreaterThanToken:
@@ -5772,7 +5777,8 @@ namespace ts {
57725777
case SyntaxKind.PropertyAccessExpression:
57735778
case SyntaxKind.NonNullExpression:
57745779
case SyntaxKind.PartiallyEmittedExpression:
5775-
node = (node as CallExpression | PropertyAccessExpression | ElementAccessExpression | AsExpression | NonNullExpression | PartiallyEmittedExpression).expression;
5780+
case SyntaxKind.SatisfiesExpression:
5781+
node = (node as CallExpression | PropertyAccessExpression | ElementAccessExpression | AsExpression | NonNullExpression | PartiallyEmittedExpression | SatisfiesExpression).expression;
57765782
continue;
57775783
}
57785784

Diff for: src/compiler/utilitiesPublic.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,7 @@ namespace ts {
15921592
case SyntaxKind.OmittedExpression:
15931593
case SyntaxKind.CommaListExpression:
15941594
case SyntaxKind.PartiallyEmittedExpression:
1595+
case SyntaxKind.SatisfiesExpression:
15951596
return true;
15961597
default:
15971598
return isUnaryExpressionKind(kind);

Diff for: src/compiler/visitorPublic.ts

+6
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,12 @@ namespace ts {
851851
nodeVisitor(node.expression, visitor, isExpression),
852852
nodeVisitor(node.type, visitor, isTypeNode));
853853

854+
case SyntaxKind.SatisfiesExpression:
855+
Debug.type<SatisfiesExpression>(node);
856+
return factory.updateSatisfiesExpression(node,
857+
nodeVisitor(node.expression, visitor, isExpression),
858+
nodeVisitor(node.type, visitor, isTypeNode));
859+
854860
case SyntaxKind.NonNullExpression:
855861
if (node.flags & NodeFlags.OptionalChain) {
856862
Debug.type<NonNullChain>(node);

Diff for: src/harness/fourslashInterfaceImpl.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ namespace FourSlashInterface {
13461346
"package",
13471347
"readonly",
13481348
"return",
1349+
"satisfies",
13491350
"string",
13501351
"super",
13511352
"switch",
@@ -1461,6 +1462,7 @@ namespace FourSlashInterface {
14611462
"null",
14621463
"package",
14631464
"return",
1465+
"satisfies",
14641466
"super",
14651467
"switch",
14661468
"this",
@@ -1558,6 +1560,7 @@ namespace FourSlashInterface {
15581560
"package",
15591561
"readonly",
15601562
"return",
1563+
"satisfies",
15611564
"string",
15621565
"super",
15631566
"switch",
@@ -1612,6 +1615,7 @@ namespace FourSlashInterface {
16121615
"null",
16131616
"package",
16141617
"return",
1618+
"satisfies",
16151619
"super",
16161620
"switch",
16171621
"this",

0 commit comments

Comments
 (0)