Skip to content

Commit 20fc192

Browse files
committed
Handle satisfies expressions when matching references
1 parent 4d66eb7 commit 20fc192

10 files changed

Lines changed: 112 additions & 1 deletion

File tree

tsc/internal/checker/flow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ func (c *Checker) reportFlowControlError(node *ast.Node) {
15961596

15971597
func (c *Checker) isMatchingReference(source *ast.Node, target *ast.Node) bool {
15981598
switch target.Kind {
1599-
case ast.KindParenthesizedExpression, ast.KindNonNullExpression:
1599+
case ast.KindParenthesizedExpression, ast.KindNonNullExpression, ast.KindSatisfiesExpression:
16001600
return c.isMatchingReference(source, target.Expression())
16011601
case ast.KindBinaryExpression:
16021602
return ast.IsAssignmentExpression(target, false) && c.isMatchingReference(source, target.AsBinaryExpression().Left) ||

tsc/testdata/baselines/reference/compiler/narrowByEquality.errors.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,9 @@ narrowByEquality.ts(55,9): error TS2322: Type 'string | number' is not assignabl
8686
xUnknown;
8787
}
8888

89+
declare let option: { type: string } | undefined;
90+
91+
if ((option satisfies { type: string } | undefined) !== undefined) {
92+
option.type;
93+
}
8994

tsc/testdata/baselines/reference/compiler/narrowByEquality.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ if (xUnknown != null) {
7373
xUnknown;
7474
}
7575

76+
declare let option: { type: string } | undefined;
77+
78+
if ((option satisfies { type: string } | undefined) !== undefined) {
79+
option.type;
80+
}
7681

7782

7883
//// [narrowByEquality.js]
@@ -129,3 +134,6 @@ if (xUnknown != null) {
129134
else {
130135
xUnknown;
131136
}
137+
if (option !== undefined) {
138+
option.type;
139+
}

tsc/testdata/baselines/reference/compiler/narrowByEquality.symbols

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,18 @@ if (xUnknown != null) {
139139
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))
140140
}
141141

142+
declare let option: { type: string } | undefined;
143+
>option : Symbol(option, Decl(narrowByEquality.ts, 72, 11))
144+
>type : Symbol(type, Decl(narrowByEquality.ts, 72, 21))
145+
146+
if ((option satisfies { type: string } | undefined) !== undefined) {
147+
>option : Symbol(option, Decl(narrowByEquality.ts, 72, 11))
148+
>type : Symbol(type, Decl(narrowByEquality.ts, 74, 23))
149+
>undefined : Symbol(undefined)
150+
151+
option.type;
152+
>option.type : Symbol(type, Decl(narrowByEquality.ts, 72, 21))
153+
>option : Symbol(option, Decl(narrowByEquality.ts, 72, 11))
154+
>type : Symbol(type, Decl(narrowByEquality.ts, 72, 21))
155+
}
142156

tsc/testdata/baselines/reference/compiler/narrowByEquality.types

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,21 @@ if (xUnknown != null) {
160160
>xUnknown : null | undefined
161161
}
162162

163+
declare let option: { type: string } | undefined;
164+
>option : { type: string; } | undefined
165+
>type : string
166+
167+
if ((option satisfies { type: string } | undefined) !== undefined) {
168+
>(option satisfies { type: string } | undefined) !== undefined : boolean
169+
>(option satisfies { type: string } | undefined) : { type: string; } | undefined
170+
>option satisfies { type: string } | undefined : { type: string; } | undefined
171+
>option : { type: string; } | undefined
172+
>type : string
173+
>undefined : undefined
174+
175+
option.type;
176+
>option.type : string
177+
>option : { type: string; }
178+
>type : string
179+
}
163180

tsc/testdata/baselines/reference/conformance/controlFlowOptionalChain4.errors.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,12 @@ controlFlowOptionalChain4.ts(70,9): error TS18048: 'option' is possibly 'undefin
8282
!!! error TS18048: 'option' is possibly 'undefined'.
8383
}
8484
}
85+
86+
function testAssignmentWithSatisfies() {
87+
let option: Option | undefined;
88+
89+
if ((option = someOptionalOption() satisfies Option | undefined)?.type === "Some") {
90+
option.value;
91+
}
92+
}
8593

tsc/testdata/baselines/reference/conformance/controlFlowOptionalChain4.symbols

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,24 @@ function testFalsyBranch() {
196196
}
197197
}
198198

199+
function testAssignmentWithSatisfies() {
200+
>testAssignmentWithSatisfies : Symbol(testAssignmentWithSatisfies, Decl(controlFlowOptionalChain4.ts, 71, 1))
201+
202+
let option: Option | undefined;
203+
>option : Symbol(option, Decl(controlFlowOptionalChain4.ts, 74, 7))
204+
>Option : Symbol(Option, Decl(lib.dom.d.ts, --, --), Decl(controlFlowOptionalChain4.ts, 31, 1))
205+
206+
if ((option = someOptionalOption() satisfies Option | undefined)?.type === "Some") {
207+
>(option = someOptionalOption() satisfies Option | undefined)?.type : Symbol(type, Decl(controlFlowOptionalChain4.ts, 35, 15), Decl(controlFlowOptionalChain4.ts, 35, 50))
208+
>option : Symbol(option, Decl(controlFlowOptionalChain4.ts, 74, 7))
209+
>someOptionalOption : Symbol(someOptionalOption, Decl(controlFlowOptionalChain4.ts, 35, 67))
210+
>Option : Symbol(Option, Decl(lib.dom.d.ts, --, --), Decl(controlFlowOptionalChain4.ts, 31, 1))
211+
>type : Symbol(type, Decl(controlFlowOptionalChain4.ts, 35, 15), Decl(controlFlowOptionalChain4.ts, 35, 50))
212+
213+
option.value;
214+
>option.value : Symbol(value, Decl(controlFlowOptionalChain4.ts, 35, 29))
215+
>option : Symbol(option, Decl(controlFlowOptionalChain4.ts, 74, 7))
216+
>value : Symbol(value, Decl(controlFlowOptionalChain4.ts, 35, 29))
217+
}
218+
}
219+

tsc/testdata/baselines/reference/conformance/controlFlowOptionalChain4.types

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,28 @@ function testFalsyBranch() {
217217
}
218218
}
219219

220+
function testAssignmentWithSatisfies() {
221+
>testAssignmentWithSatisfies : () => void
222+
223+
let option: Option | undefined;
224+
>option : Option | undefined
225+
226+
if ((option = someOptionalOption() satisfies Option | undefined)?.type === "Some") {
227+
>(option = someOptionalOption() satisfies Option | undefined)?.type === "Some" : boolean
228+
>(option = someOptionalOption() satisfies Option | undefined)?.type : "None" | "Some" | undefined
229+
>(option = someOptionalOption() satisfies Option | undefined) : Option | undefined
230+
>option = someOptionalOption() satisfies Option | undefined : Option | undefined
231+
>option : Option | undefined
232+
>someOptionalOption() satisfies Option | undefined : Option | undefined
233+
>someOptionalOption() : Option | undefined
234+
>someOptionalOption : () => Option | undefined
235+
>type : "None" | "Some" | undefined
236+
>"Some" : "Some"
237+
238+
option.value;
239+
>option.value : number
240+
>option : { type: "Some"; value: number; }
241+
>value : number
242+
}
243+
}
244+

tsc/testdata/tests/cases/compiler/narrowByEquality.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,8 @@ if (xUnknown != null) {
7373
xUnknown;
7474
}
7575

76+
declare let option: { type: string } | undefined;
77+
78+
if ((option satisfies { type: string } | undefined) !== undefined) {
79+
option.type;
80+
}

tsc/testdata/tests/cases/conformance/controlFlow/controlFlowOptionalChain4.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@ function testFalsyBranch() {
7373
option.type;
7474
}
7575
}
76+
77+
function testAssignmentWithSatisfies() {
78+
let option: Option | undefined;
79+
80+
if ((option = someOptionalOption() satisfies Option | undefined)?.type === "Some") {
81+
option.value;
82+
}
83+
}

0 commit comments

Comments
 (0)