-
Notifications
You must be signed in to change notification settings - Fork 582
/
Copy pathflow.go
2714 lines (2580 loc) · 110 KB
/
flow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package checker
import (
"math"
"slices"
"strconv"
"strings"
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/binder"
"github.com/microsoft/typescript-go/internal/compiler/diagnostics"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/evaluator"
"github.com/microsoft/typescript-go/internal/scanner"
)
type FlowType struct {
t *Type
incomplete bool
}
func (ft *FlowType) isNil() bool {
return ft.t == nil
}
func (c *Checker) newFlowType(t *Type, incomplete bool) FlowType {
if incomplete && t.flags&TypeFlagsNever != 0 {
t = c.silentNeverType
}
return FlowType{t: t, incomplete: incomplete}
}
type SharedFlow struct {
flow *ast.FlowNode
flowType FlowType
}
type FlowState struct {
reference *ast.Node
declaredType *Type
initialType *Type
flowContainer *ast.Node
refKey string
depth int
sharedFlowStart int
reduceLabels []*ast.FlowReduceLabelData
next *FlowState
}
func (c *Checker) getFlowState() *FlowState {
f := c.freeFlowState
if f == nil {
f = &FlowState{}
}
c.freeFlowState = f.next
return f
}
func (c *Checker) putFlowState(f *FlowState) {
*f = FlowState{
reduceLabels: f.reduceLabels[:0],
next: c.freeFlowState,
}
c.freeFlowState = f
}
func getFlowNodeOfNode(node *ast.Node) *ast.FlowNode {
flowNodeData := node.FlowNodeData()
if flowNodeData != nil {
return flowNodeData.FlowNode
}
return nil
}
func (c *Checker) getFlowTypeOfReference(reference *ast.Node, declaredType *Type) *Type {
return c.getFlowTypeOfReferenceEx(reference, declaredType, declaredType, nil, nil)
}
func (c *Checker) getFlowTypeOfReferenceEx(reference *ast.Node, declaredType *Type, initialType *Type, flowContainer *ast.Node, flowNode *ast.FlowNode) *Type {
if c.flowAnalysisDisabled {
return c.errorType
}
if flowNode == nil {
flowNode = getFlowNodeOfNode(reference)
if flowNode == nil {
return declaredType
}
}
f := c.getFlowState()
f.reference = reference
f.declaredType = declaredType
f.initialType = core.Coalesce(initialType, declaredType)
f.flowContainer = flowContainer
f.sharedFlowStart = len(c.sharedFlows)
c.flowInvocationCount++
evolvedType := c.getTypeAtFlowNode(f, flowNode).t
c.sharedFlows = c.sharedFlows[:f.sharedFlowStart]
c.putFlowState(f)
// When the reference is 'x' in an 'x.length', 'x.push(value)', 'x.unshift(value)' or x[n] = value' operation,
// we give type 'any[]' to 'x' instead of using the type determined by control flow analysis such that operations
// on empty arrays are possible without implicit any errors and new element types can be inferred without
// type mismatch errors.
var resultType *Type
if evolvedType.objectFlags&ObjectFlagsEvolvingArray != 0 && c.isEvolvingArrayOperationTarget(reference) {
resultType = c.autoArrayType
} else {
resultType = c.finalizeEvolvingArrayType(evolvedType)
}
if resultType == c.unreachableNeverType || reference.Parent != nil && ast.IsNonNullExpression(reference.Parent) && resultType.flags&TypeFlagsNever == 0 && c.getTypeWithFacts(resultType, TypeFactsNEUndefinedOrNull).flags&TypeFlagsNever != 0 {
return declaredType
}
return resultType
}
func (c *Checker) getTypeAtFlowNode(f *FlowState, flow *ast.FlowNode) FlowType {
if f.depth == 2000 {
// We have made 2000 recursive invocations. To avoid overflowing the call stack we report an error
// and disable further control flow analysis in the containing function or module body.
c.flowAnalysisDisabled = true
c.reportFlowControlError(f.reference)
return FlowType{t: c.errorType}
}
f.depth++
var sharedFlow *ast.FlowNode
for {
flags := flow.Flags
if flags&ast.FlowFlagsShared != 0 {
// We cache results of flow type resolution for shared nodes that were previously visited in
// the same getFlowTypeOfReference invocation. A node is considered shared when it is the
// antecedent of more than one node.
for i := f.sharedFlowStart; i < len(c.sharedFlows); i++ {
if c.sharedFlows[i].flow == flow {
f.depth--
return c.sharedFlows[i].flowType
}
}
sharedFlow = flow
}
var t FlowType
switch {
case flags&ast.FlowFlagsAssignment != 0:
t = c.getTypeAtFlowAssignment(f, flow)
if t.isNil() {
flow = flow.Antecedent
continue
}
case flags&ast.FlowFlagsCall != 0:
t = c.getTypeAtFlowCall(f, flow)
if t.isNil() {
flow = flow.Antecedent
continue
}
case flags&ast.FlowFlagsCondition != 0:
t = c.getTypeAtFlowCondition(f, flow)
case flags&ast.FlowFlagsSwitchClause != 0:
t = c.getTypeAtSwitchClause(f, flow)
case flags&ast.FlowFlagsBranchLabel != 0:
antecedents := getBranchLabelAntecedents(flow, f.reduceLabels)
if antecedents.Next == nil {
flow = antecedents.Flow
continue
}
t = c.getTypeAtFlowBranchLabel(f, flow, antecedents)
case flags&ast.FlowFlagsLoopLabel != 0:
if flow.Antecedents.Next == nil {
flow = flow.Antecedents.Flow
continue
}
t = c.getTypeAtFlowLoopLabel(f, flow)
case flags&ast.FlowFlagsArrayMutation != 0:
t = c.getTypeAtFlowArrayMutation(f, flow)
if t.isNil() {
flow = flow.Antecedent
continue
}
case flags&ast.FlowFlagsReduceLabel != 0:
f.reduceLabels = append(f.reduceLabels, flow.Node.AsFlowReduceLabelData())
t = c.getTypeAtFlowNode(f, flow.Antecedent)
f.reduceLabels = f.reduceLabels[:len(f.reduceLabels)-1]
case flags&ast.FlowFlagsStart != 0:
// Check if we should continue with the control flow of the containing function.
container := flow.Node
if container != nil && container != f.flowContainer && !ast.IsPropertyAccessExpression(f.reference) && !ast.IsElementAccessExpression(f.reference) && !(f.reference.Kind == ast.KindThisKeyword && !ast.IsArrowFunction(container)) {
flow = container.FlowNodeData().FlowNode
continue
}
// At the top of the flow we have the initial type.
t = FlowType{t: f.initialType}
default:
// Unreachable code errors are reported in the binding phase. Here we
// simply return the non-auto declared type to reduce follow-on errors.
t = FlowType{t: c.convertAutoToAny(f.declaredType)}
}
if sharedFlow != nil {
// Record visited node and the associated type in the cache.
c.sharedFlows = append(c.sharedFlows, SharedFlow{flow: sharedFlow, flowType: t})
}
f.depth--
return t
}
}
func getBranchLabelAntecedents(flow *ast.FlowNode, reduceLabels []*ast.FlowReduceLabelData) *ast.FlowList {
i := len(reduceLabels)
for i != 0 {
i--
data := reduceLabels[i]
if data.Target == flow {
return data.Antecedents
}
}
return flow.Antecedents
}
func (c *Checker) getTypeAtFlowAssignment(f *FlowState, flow *ast.FlowNode) FlowType {
node := flow.Node
// Assignments only narrow the computed type if the declared type is a union type. Thus, we
// only need to evaluate the assigned type if the declared type is a union type.
if c.isMatchingReference(f.reference, node) {
if !c.isReachableFlowNode(flow) {
return FlowType{t: c.unreachableNeverType}
}
if getAssignmentTargetKind(node) == AssignmentKindCompound {
flowType := c.getTypeAtFlowNode(f, flow.Antecedent)
return c.newFlowType(c.getBaseTypeOfLiteralType(flowType.t), flowType.incomplete)
}
if f.declaredType == c.autoType || f.declaredType == c.autoArrayType {
if c.isEmptyArrayAssignment(node) {
return FlowType{t: c.getEvolvingArrayType(c.neverType)}
}
assignedType := c.getWidenedLiteralType(c.getInitialOrAssignedType(f, flow))
if c.isTypeAssignableTo(assignedType, f.declaredType) {
return FlowType{t: assignedType}
}
return FlowType{t: c.anyArrayType}
}
t := f.declaredType
if isInCompoundLikeAssignment(node) {
t = c.getBaseTypeOfLiteralType(t)
}
if t.flags&TypeFlagsUnion != 0 {
return FlowType{t: c.getAssignmentReducedType(t, c.getInitialOrAssignedType(f, flow))}
}
return FlowType{t: t}
}
// We didn't have a direct match. However, if the reference is a dotted name, this
// may be an assignment to a left hand part of the reference. For example, for a
// reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case,
// return the declared type.
if c.containsMatchingReference(f.reference, node) {
if !c.isReachableFlowNode(flow) {
return FlowType{t: c.unreachableNeverType}
}
return FlowType{t: f.declaredType}
}
// for (const _ in ref) acts as a nonnull on ref
if ast.IsVariableDeclaration(node) && ast.IsForInStatement(node.Parent.Parent) && (c.isMatchingReference(f.reference, node.Parent.Parent.Expression()) || c.optionalChainContainsReference(node.Parent.Parent.Expression(), f.reference)) {
return FlowType{t: c.getNonNullableTypeIfNeeded(c.finalizeEvolvingArrayType(c.getTypeAtFlowNode(f, flow.Antecedent).t))}
}
// Assignment doesn't affect reference
return FlowType{}
}
func (c *Checker) getInitialOrAssignedType(f *FlowState, flow *ast.FlowNode) *Type {
if ast.IsVariableDeclaration(flow.Node) || ast.IsBindingElement(flow.Node) {
return c.getNarrowableTypeForReference(c.getInitialType(flow.Node), f.reference, CheckModeNormal)
}
return c.getNarrowableTypeForReference(c.getAssignedType(flow.Node), f.reference, CheckModeNormal)
}
func (c *Checker) isEmptyArrayAssignment(node *ast.Node) bool {
return ast.IsVariableDeclaration(node) && node.Initializer() != nil && isEmptyArrayLiteral(node.Initializer()) ||
!ast.IsBindingElement(node) && ast.IsBinaryExpression(node.Parent) && isEmptyArrayLiteral(node.Parent.AsBinaryExpression().Right)
}
func (c *Checker) getTypeAtFlowCall(f *FlowState, flow *ast.FlowNode) FlowType {
signature := c.getEffectsSignature(flow.Node)
if signature != nil {
predicate := c.getTypePredicateOfSignature(signature)
if predicate != nil && (predicate.kind == TypePredicateKindAssertsThis || predicate.kind == TypePredicateKindAssertsIdentifier) {
flowType := c.getTypeAtFlowNode(f, flow.Antecedent)
t := c.finalizeEvolvingArrayType(flowType.t)
var narrowedType *Type
switch {
case predicate.t != nil:
narrowedType = c.narrowTypeByTypePredicate(f, t, predicate, flow.Node, true /*assumeTrue*/)
case predicate.kind == TypePredicateKindAssertsIdentifier && predicate.parameterIndex >= 0 && int(predicate.parameterIndex) < len(flow.Node.Arguments()):
narrowedType = c.narrowTypeByAssertion(f, t, flow.Node.Arguments()[predicate.parameterIndex])
default:
narrowedType = t
}
if narrowedType == t {
return flowType
}
return c.newFlowType(narrowedType, flowType.incomplete)
}
if c.getReturnTypeOfSignature(signature).flags&TypeFlagsNever != 0 {
return FlowType{t: c.unreachableNeverType}
}
}
return FlowType{}
}
func (c *Checker) narrowTypeByTypePredicate(f *FlowState, t *Type, predicate *TypePredicate, callExpression *ast.Node, assumeTrue bool) *Type {
// Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function'
if predicate.t != nil && !(IsTypeAny(t) && (predicate.t == c.globalObjectType || predicate.t == c.globalFunctionType)) {
predicateArgument := c.getTypePredicateArgument(predicate, callExpression)
if predicateArgument != nil {
if c.isMatchingReference(f.reference, predicateArgument) {
return c.getNarrowedType(t, predicate.t, assumeTrue, false /*checkDerived*/)
}
if c.strictNullChecks && c.optionalChainContainsReference(predicateArgument, f.reference) && (assumeTrue && !(c.hasTypeFacts(predicate.t, TypeFactsEQUndefined)) || !assumeTrue && everyType(predicate.t, c.isNullableType)) {
t = c.getAdjustedTypeWithFacts(t, TypeFactsNEUndefinedOrNull)
}
access := c.getDiscriminantPropertyAccess(f, predicateArgument, t)
if access != nil {
return c.narrowTypeByDiscriminant(t, access, func(t *Type) *Type {
return c.getNarrowedType(t, predicate.t, assumeTrue, false /*checkDerived*/)
})
}
}
}
return t
}
func (c *Checker) narrowTypeByAssertion(f *FlowState, t *Type, expr *ast.Node) *Type {
node := ast.SkipParentheses(expr)
if node.Kind == ast.KindFalseKeyword {
return c.unreachableNeverType
}
if node.Kind == ast.KindBinaryExpression {
if node.AsBinaryExpression().OperatorToken.Kind == ast.KindAmpersandAmpersandToken {
return c.narrowTypeByAssertion(f, c.narrowTypeByAssertion(f, t, node.AsBinaryExpression().Left), node.AsBinaryExpression().Right)
}
if node.AsBinaryExpression().OperatorToken.Kind == ast.KindBarBarToken {
return c.getUnionType([]*Type{c.narrowTypeByAssertion(f, t, node.AsBinaryExpression().Left), c.narrowTypeByAssertion(f, t, node.AsBinaryExpression().Right)})
}
}
return c.narrowType(f, t, node, true /*assumeTrue*/)
}
func (c *Checker) getTypeAtFlowCondition(f *FlowState, flow *ast.FlowNode) FlowType {
flowType := c.getTypeAtFlowNode(f, flow.Antecedent)
if flowType.t.flags&TypeFlagsNever != 0 {
return flowType
}
// If we have an antecedent type (meaning we're reachable in some way), we first
// attempt to narrow the antecedent type. If that produces the never type, and if
// the antecedent type is incomplete (i.e. a transient type in a loop), then we
// take the type guard as an indication that control *could* reach here once we
// have the complete type. We proceed by switching to the silent never type which
// doesn't report errors when operators are applied to it. Note that this is the
// *only* place a silent never type is ever generated.
assumeTrue := flow.Flags&ast.FlowFlagsTrueCondition != 0
nonEvolvingType := c.finalizeEvolvingArrayType(flowType.t)
narrowedType := c.narrowType(f, nonEvolvingType, flow.Node, assumeTrue)
if narrowedType == nonEvolvingType {
return flowType
}
return c.newFlowType(narrowedType, flowType.incomplete)
}
// Narrow the given type based on the given expression having the assumed boolean value. The returned type
// will be a subtype or the same type as the argument.
func (c *Checker) narrowType(f *FlowState, t *Type, expr *ast.Node, assumeTrue bool) *Type {
// for `a?.b`, we emulate a synthetic `a !== null && a !== undefined` condition for `a`
if ast.IsExpressionOfOptionalChainRoot(expr) || ast.IsBinaryExpression(expr.Parent) && (expr.Parent.AsBinaryExpression().OperatorToken.Kind == ast.KindQuestionQuestionToken || expr.Parent.AsBinaryExpression().OperatorToken.Kind == ast.KindQuestionQuestionEqualsToken) && expr.Parent.AsBinaryExpression().Left == expr {
return c.narrowTypeByOptionality(f, t, expr, assumeTrue)
}
switch expr.Kind {
case ast.KindIdentifier:
// When narrowing a reference to a const variable, non-assigned parameter, or readonly property, we inline
// up to five levels of aliased conditional expressions that are themselves declared as const variables.
if !c.isMatchingReference(f.reference, expr) && c.inlineLevel < 5 {
symbol := c.getResolvedSymbol(expr)
if c.isConstantVariable(symbol) {
declaration := symbol.ValueDeclaration
if declaration != nil && ast.IsVariableDeclaration(declaration) && declaration.Type() == nil && declaration.Initializer() != nil && c.isConstantReference(f.reference) {
c.inlineLevel++
result := c.narrowType(f, t, declaration.Initializer(), assumeTrue)
c.inlineLevel--
return result
}
}
}
fallthrough
case ast.KindThisKeyword, ast.KindSuperKeyword, ast.KindPropertyAccessExpression, ast.KindElementAccessExpression:
return c.narrowTypeByTruthiness(f, t, expr, assumeTrue)
case ast.KindCallExpression:
return c.narrowTypeByCallExpression(f, t, expr, assumeTrue)
case ast.KindParenthesizedExpression, ast.KindNonNullExpression:
return c.narrowType(f, t, expr.Expression(), assumeTrue)
case ast.KindBinaryExpression:
return c.narrowTypeByBinaryExpression(f, t, expr.AsBinaryExpression(), assumeTrue)
case ast.KindPrefixUnaryExpression:
if expr.AsPrefixUnaryExpression().Operator == ast.KindExclamationToken {
return c.narrowType(f, t, expr.AsPrefixUnaryExpression().Operand, !assumeTrue)
}
}
return t
}
func (c *Checker) narrowTypeByOptionality(f *FlowState, t *Type, expr *ast.Node, assumePresent bool) *Type {
if c.isMatchingReference(f.reference, expr) {
return c.getAdjustedTypeWithFacts(t, core.IfElse(assumePresent, TypeFactsNEUndefinedOrNull, TypeFactsEQUndefinedOrNull))
}
access := c.getDiscriminantPropertyAccess(f, expr, t)
if access != nil {
return c.narrowTypeByDiscriminant(t, access, func(t *Type) *Type {
return c.getTypeWithFacts(t, core.IfElse(assumePresent, TypeFactsNEUndefinedOrNull, TypeFactsEQUndefinedOrNull))
})
}
return t
}
func (c *Checker) narrowTypeByTruthiness(f *FlowState, t *Type, expr *ast.Node, assumeTrue bool) *Type {
if c.isMatchingReference(f.reference, expr) {
return c.getAdjustedTypeWithFacts(t, core.IfElse(assumeTrue, TypeFactsTruthy, TypeFactsFalsy))
}
if c.strictNullChecks && assumeTrue && c.optionalChainContainsReference(expr, f.reference) {
t = c.getAdjustedTypeWithFacts(t, TypeFactsNEUndefinedOrNull)
}
access := c.getDiscriminantPropertyAccess(f, expr, t)
if access != nil {
return c.narrowTypeByDiscriminant(t, access, func(t *Type) *Type {
return c.getTypeWithFacts(t, core.IfElse(assumeTrue, TypeFactsTruthy, TypeFactsFalsy))
})
}
return t
}
func (c *Checker) narrowTypeByCallExpression(f *FlowState, t *Type, callExpression *ast.Node, assumeTrue bool) *Type {
if c.hasMatchingArgument(callExpression, f.reference) {
var predicate *TypePredicate
if assumeTrue || !isCallChain(callExpression) {
signature := c.getEffectsSignature(callExpression)
if signature != nil {
predicate = c.getTypePredicateOfSignature(signature)
}
}
if predicate != nil && (predicate.kind == TypePredicateKindThis || predicate.kind == TypePredicateKindIdentifier) {
return c.narrowTypeByTypePredicate(f, t, predicate, callExpression, assumeTrue)
}
}
if c.containsMissingType(t) && ast.IsAccessExpression(f.reference) && ast.IsPropertyAccessExpression(callExpression.Expression()) {
callAccess := callExpression.Expression()
if c.isMatchingReference(f.reference.Expression(), c.getReferenceCandidate(callAccess.Expression())) && ast.IsIdentifier(callAccess.Name()) && callAccess.Name().Text() == "hasOwnProperty" && len(callExpression.Arguments()) == 1 {
argument := callExpression.Arguments()[0]
if accessedName, ok := c.getAccessedPropertyName(f.reference); ok && ast.IsStringLiteralLike(argument) && accessedName == argument.Text() {
return c.getTypeWithFacts(t, core.IfElse(assumeTrue, TypeFactsNEUndefined, TypeFactsEQUndefined))
}
}
}
return t
}
func (c *Checker) narrowTypeByBinaryExpression(f *FlowState, t *Type, expr *ast.BinaryExpression, assumeTrue bool) *Type {
switch expr.OperatorToken.Kind {
case ast.KindEqualsToken, ast.KindBarBarEqualsToken, ast.KindAmpersandAmpersandEqualsToken, ast.KindQuestionQuestionEqualsToken:
return c.narrowTypeByTruthiness(f, c.narrowType(f, t, expr.Right, assumeTrue), expr.Left, assumeTrue)
case ast.KindEqualsEqualsToken, ast.KindExclamationEqualsToken, ast.KindEqualsEqualsEqualsToken, ast.KindExclamationEqualsEqualsToken:
operator := expr.OperatorToken.Kind
left := c.getReferenceCandidate(expr.Left)
right := c.getReferenceCandidate(expr.Right)
if left.Kind == ast.KindTypeOfExpression && ast.IsStringLiteralLike(right) {
return c.narrowTypeByTypeof(f, t, left.AsTypeOfExpression(), operator, right, assumeTrue)
}
if right.Kind == ast.KindTypeOfExpression && ast.IsStringLiteralLike(left) {
return c.narrowTypeByTypeof(f, t, right.AsTypeOfExpression(), operator, left, assumeTrue)
}
if c.isMatchingReference(f.reference, left) {
return c.narrowTypeByEquality(t, operator, right, assumeTrue)
}
if c.isMatchingReference(f.reference, right) {
return c.narrowTypeByEquality(t, operator, left, assumeTrue)
}
if c.strictNullChecks {
if c.optionalChainContainsReference(left, f.reference) {
t = c.narrowTypeByOptionalChainContainment(f, t, operator, right, assumeTrue)
} else if c.optionalChainContainsReference(right, f.reference) {
t = c.narrowTypeByOptionalChainContainment(f, t, operator, left, assumeTrue)
}
}
leftAccess := c.getDiscriminantPropertyAccess(f, left, t)
if leftAccess != nil {
return c.narrowTypeByDiscriminantProperty(t, leftAccess, operator, right, assumeTrue)
}
rightAccess := c.getDiscriminantPropertyAccess(f, right, t)
if rightAccess != nil {
return c.narrowTypeByDiscriminantProperty(t, rightAccess, operator, left, assumeTrue)
}
if c.isMatchingConstructorReference(f, left) {
return c.narrowTypeByConstructor(t, operator, right, assumeTrue)
}
if c.isMatchingConstructorReference(f, right) {
return c.narrowTypeByConstructor(t, operator, left, assumeTrue)
}
if ast.IsBooleanLiteral(right) && !ast.IsAccessExpression(left) {
return c.narrowTypeByBooleanComparison(f, t, left, right, operator, assumeTrue)
}
if ast.IsBooleanLiteral(left) && !ast.IsAccessExpression(right) {
return c.narrowTypeByBooleanComparison(f, t, right, left, operator, assumeTrue)
}
case ast.KindInstanceOfKeyword:
return c.narrowTypeByInstanceof(f, t, expr, assumeTrue)
case ast.KindInKeyword:
if ast.IsPrivateIdentifier(expr.Left) {
return c.narrowTypeByPrivateIdentifierInInExpression(f, t, expr, assumeTrue)
}
target := c.getReferenceCandidate(expr.Right)
if c.containsMissingType(t) && ast.IsAccessExpression(f.reference) && c.isMatchingReference(f.reference.Expression(), target) {
leftType := c.getTypeOfExpression(expr.Left)
if isTypeUsableAsPropertyName(leftType) {
if accessedName, ok := c.getAccessedPropertyName(f.reference); ok && accessedName == getPropertyNameFromType(leftType) {
return c.getTypeWithFacts(t, core.IfElse(assumeTrue, TypeFactsNEUndefined, TypeFactsEQUndefined))
}
}
}
if c.isMatchingReference(f.reference, target) {
leftType := c.getTypeOfExpression(expr.Left)
if isTypeUsableAsPropertyName(leftType) {
return c.narrowTypeByInKeyword(f, t, leftType, assumeTrue)
}
}
case ast.KindCommaToken:
return c.narrowType(f, t, expr.Right, assumeTrue)
case ast.KindAmpersandAmpersandToken:
// Ordinarily we won't see && and || expressions in control flow analysis because the Binder breaks those
// expressions down to individual conditional control flows. However, we may encounter them when analyzing
// aliased conditional expressions.
if assumeTrue {
return c.narrowType(f, c.narrowType(f, t, expr.Left, true /*assumeTrue*/), expr.Right, true /*assumeTrue*/)
}
return c.getUnionType([]*Type{c.narrowType(f, t, expr.Left, false /*assumeTrue*/), c.narrowType(f, t, expr.Right, false /*assumeTrue*/)})
case ast.KindBarBarToken:
if assumeTrue {
return c.getUnionType([]*Type{c.narrowType(f, t, expr.Left, true /*assumeTrue*/), c.narrowType(f, t, expr.Right, true /*assumeTrue*/)})
}
return c.narrowType(f, c.narrowType(f, t, expr.Left, false /*assumeTrue*/), expr.Right, false /*assumeTrue*/)
}
return t
}
func (c *Checker) narrowTypeByEquality(t *Type, operator ast.Kind, value *ast.Node, assumeTrue bool) *Type {
if t.flags&TypeFlagsAny != 0 {
return t
}
if operator == ast.KindExclamationEqualsToken || operator == ast.KindExclamationEqualsEqualsToken {
assumeTrue = !assumeTrue
}
valueType := c.getTypeOfExpression(value)
doubleEquals := operator == ast.KindEqualsEqualsToken || operator == ast.KindExclamationEqualsToken
if valueType.flags&TypeFlagsNullable != 0 {
if !c.strictNullChecks {
return t
}
var facts TypeFacts
switch {
case doubleEquals:
facts = core.IfElse(assumeTrue, TypeFactsEQUndefinedOrNull, TypeFactsNEUndefinedOrNull)
case valueType.flags&TypeFlagsNull != 0:
facts = core.IfElse(assumeTrue, TypeFactsEQNull, TypeFactsNENull)
default:
facts = core.IfElse(assumeTrue, TypeFactsEQUndefined, TypeFactsNEUndefined)
}
return c.getAdjustedTypeWithFacts(t, facts)
}
if assumeTrue {
if !doubleEquals && (t.flags&TypeFlagsUnknown != 0 || someType(t, c.isEmptyAnonymousObjectType)) {
if valueType.flags&(TypeFlagsPrimitive|TypeFlagsNonPrimitive) != 0 || c.isEmptyAnonymousObjectType(valueType) {
return valueType
}
if valueType.flags&TypeFlagsObject != 0 {
return c.nonPrimitiveType
}
}
filteredType := c.filterType(t, func(t *Type) bool {
return c.areTypesComparable(t, valueType) || doubleEquals && isCoercibleUnderDoubleEquals(t, valueType)
})
return c.replacePrimitivesWithLiterals(filteredType, valueType)
}
if isUnitType(valueType) {
return c.filterType(t, func(t *Type) bool {
return !(c.isUnitLikeType(t) && c.areTypesComparable(t, valueType))
})
}
return t
}
func (c *Checker) narrowTypeByTypeof(f *FlowState, t *Type, typeOfExpr *ast.TypeOfExpression, operator ast.Kind, literal *ast.Node, assumeTrue bool) *Type {
// We have '==', '!=', '===', or !==' operator with 'typeof xxx' and string literal operands
if operator == ast.KindExclamationEqualsToken || operator == ast.KindExclamationEqualsEqualsToken {
assumeTrue = !assumeTrue
}
target := c.getReferenceCandidate(typeOfExpr.Expression)
if !c.isMatchingReference(f.reference, target) {
if c.strictNullChecks && c.optionalChainContainsReference(target, f.reference) && assumeTrue == (literal.Text() != "undefined") {
t = c.getAdjustedTypeWithFacts(t, TypeFactsNEUndefinedOrNull)
}
propertyAccess := c.getDiscriminantPropertyAccess(f, target, t)
if propertyAccess != nil {
return c.narrowTypeByDiscriminant(t, propertyAccess, func(t *Type) *Type {
return c.narrowTypeByLiteralExpression(t, literal, assumeTrue)
})
}
return t
}
return c.narrowTypeByLiteralExpression(t, literal, assumeTrue)
}
var typeofNEFacts = map[string]TypeFacts{
"string": TypeFactsTypeofNEString,
"number": TypeFactsTypeofNENumber,
"bigint": TypeFactsTypeofNEBigInt,
"boolean": TypeFactsTypeofNEBoolean,
"symbol": TypeFactsTypeofNESymbol,
"undefined": TypeFactsNEUndefined,
"object": TypeFactsTypeofNEObject,
"function": TypeFactsTypeofNEFunction,
}
func (c *Checker) narrowTypeByLiteralExpression(t *Type, literal *ast.LiteralExpression, assumeTrue bool) *Type {
if assumeTrue {
return c.narrowTypeByTypeName(t, literal.Text())
}
facts, ok := typeofNEFacts[literal.Text()]
if !ok {
facts = TypeFactsTypeofNEHostObject
}
return c.getAdjustedTypeWithFacts(t, facts)
}
func (c *Checker) narrowTypeByTypeName(t *Type, typeName string) *Type {
switch typeName {
case "string":
return c.narrowTypeByTypeFacts(t, c.stringType, TypeFactsTypeofEQString)
case "number":
return c.narrowTypeByTypeFacts(t, c.numberType, TypeFactsTypeofEQNumber)
case "bigint":
return c.narrowTypeByTypeFacts(t, c.bigintType, TypeFactsTypeofEQBigInt)
case "boolean":
return c.narrowTypeByTypeFacts(t, c.booleanType, TypeFactsTypeofEQBoolean)
case "symbol":
return c.narrowTypeByTypeFacts(t, c.esSymbolType, TypeFactsTypeofEQSymbol)
case "object":
if t.flags&TypeFlagsAny != 0 {
return t
}
return c.getUnionType([]*Type{c.narrowTypeByTypeFacts(t, c.nonPrimitiveType, TypeFactsTypeofEQObject), c.narrowTypeByTypeFacts(t, c.nullType, TypeFactsEQNull)})
case "function":
if t.flags&TypeFlagsAny != 0 {
return t
}
return c.narrowTypeByTypeFacts(t, c.globalFunctionType, TypeFactsTypeofEQFunction)
case "undefined":
return c.narrowTypeByTypeFacts(t, c.undefinedType, TypeFactsEQUndefined)
}
return c.narrowTypeByTypeFacts(t, c.nonPrimitiveType, TypeFactsTypeofEQHostObject)
}
func (c *Checker) narrowTypeByTypeFacts(t *Type, impliedType *Type, facts TypeFacts) *Type {
return c.mapType(t, func(t *Type) *Type {
switch {
case c.isTypeRelatedTo(t, impliedType, c.strictSubtypeRelation):
if c.hasTypeFacts(t, facts) {
return t
}
return c.neverType
case c.isTypeSubtypeOf(impliedType, t):
return impliedType
case c.hasTypeFacts(t, facts):
return c.getIntersectionType([]*Type{t, impliedType})
}
return c.neverType
})
}
func (c *Checker) narrowTypeByDiscriminantProperty(t *Type, access *ast.Node, operator ast.Kind, value *ast.Node, assumeTrue bool) *Type {
if (operator == ast.KindEqualsEqualsEqualsToken || operator == ast.KindExclamationEqualsEqualsToken) && t.flags&TypeFlagsUnion != 0 {
keyPropertyName := c.getKeyPropertyName(t)
if keyPropertyName != "" {
if accessedName, ok := c.getAccessedPropertyName(access); ok && keyPropertyName == accessedName {
candidate := c.getConstituentTypeForKeyType(t, c.getTypeOfExpression(value))
if candidate != nil {
if assumeTrue && operator == ast.KindEqualsEqualsEqualsToken || !assumeTrue && operator == ast.KindExclamationEqualsEqualsToken {
return candidate
}
if propType := c.getTypeOfPropertyOfType(candidate, keyPropertyName); propType != nil && isUnitType(propType) {
return c.removeType(t, candidate)
}
return t
}
}
}
}
return c.narrowTypeByDiscriminant(t, access, func(t *Type) *Type {
return c.narrowTypeByEquality(t, operator, value, assumeTrue)
})
}
func (c *Checker) narrowTypeByDiscriminant(t *Type, access *ast.Node, narrowType func(t *Type) *Type) *Type {
propName, ok := c.getAccessedPropertyName(access)
if !ok {
return t
}
optionalChain := ast.IsOptionalChain(access)
removeNullable := c.strictNullChecks && (optionalChain || isNonNullAccess(access)) && c.maybeTypeOfKind(t, TypeFlagsNullable)
nonNullType := t
if removeNullable {
nonNullType = c.getTypeWithFacts(t, TypeFactsNEUndefinedOrNull)
}
propType := c.getTypeOfPropertyOfType(nonNullType, propName)
if propType == nil {
return t
}
if removeNullable && optionalChain {
propType = c.getOptionalType(propType, false)
}
narrowedPropType := narrowType(propType)
return c.filterType(t, func(t *Type) bool {
discriminantType := c.getTypeOfPropertyOrIndexSignatureOfType(t, propName)
return discriminantType == nil || discriminantType.flags&TypeFlagsNever == 0 && narrowedPropType.flags&TypeFlagsNever == 0 && c.areTypesComparable(narrowedPropType, discriminantType)
})
}
func (c *Checker) isMatchingConstructorReference(f *FlowState, expr *ast.Node) bool {
if ast.IsAccessExpression(expr) {
if accessedName, ok := c.getAccessedPropertyName(expr); ok && accessedName == "constructor" && c.isMatchingReference(f.reference, expr.Expression()) {
return true
}
}
return false
}
func (c *Checker) narrowTypeByConstructor(t *Type, operator ast.Kind, identifier *ast.Node, assumeTrue bool) *Type {
// Do not narrow when checking inequality.
if assumeTrue && operator != ast.KindEqualsEqualsToken && operator != ast.KindEqualsEqualsEqualsToken || !assumeTrue && operator != ast.KindExclamationEqualsToken && operator != ast.KindExclamationEqualsEqualsToken {
return t
}
// Get the type of the constructor identifier expression, if it is not a function then do not narrow.
identifierType := c.getTypeOfExpression(identifier)
if !c.isFunctionType(identifierType) && !c.isConstructorType(identifierType) {
return t
}
// Get the prototype property of the type identifier so we can find out its type.
prototypeProperty := c.getPropertyOfType(identifierType, "prototype")
if prototypeProperty == nil {
return t
}
// Get the type of the prototype, if it is undefined, or the global `Object` or `Function` types then do not narrow.
prototypeType := c.getTypeOfSymbol(prototypeProperty)
var candidate *Type
if !IsTypeAny(prototypeType) {
candidate = prototypeType
}
if candidate == nil || candidate == c.globalObjectType || candidate == c.globalFunctionType {
return t
}
// If the type that is being narrowed is `any` then just return the `candidate` type since every type is a subtype of `any`.
if IsTypeAny(t) {
return candidate
}
// Filter out types that are not considered to be "constructed by" the `candidate` type.
return c.filterType(t, func(t *Type) bool {
return c.isConstructedBy(t, candidate)
})
}
func (c *Checker) isConstructedBy(source *Type, target *Type) bool {
// If either the source or target type are a class type then we need to check that they are the same exact type.
// This is because you may have a class `A` that defines some set of properties, and another class `B`
// that defines the same set of properties as class `A`, in that case they are structurally the same
// type, but when you do something like `instanceOfA.constructor === B` it will return false.
if source.flags&TypeFlagsObject != 0 && source.objectFlags&ObjectFlagsClass != 0 || target.flags&TypeFlagsObject != 0 && target.objectFlags&ObjectFlagsClass != 0 {
return source.symbol == target.symbol
}
// For all other types just check that the `source` type is a subtype of the `target` type.
return c.isTypeSubtypeOf(source, target)
}
func (c *Checker) narrowTypeByBooleanComparison(f *FlowState, t *Type, expr *ast.Node, boolValue *ast.Node, operator ast.Kind, assumeTrue bool) *Type {
assumeTrue = (assumeTrue != (boolValue.Kind == ast.KindTrueKeyword)) != (operator != ast.KindExclamationEqualsEqualsToken && operator != ast.KindExclamationEqualsToken)
return c.narrowType(f, t, expr, assumeTrue)
}
func (c *Checker) narrowTypeByInstanceof(f *FlowState, t *Type, expr *ast.BinaryExpression, assumeTrue bool) *Type {
left := c.getReferenceCandidate(expr.Left)
if !c.isMatchingReference(f.reference, left) {
if assumeTrue && c.strictNullChecks && c.optionalChainContainsReference(left, f.reference) {
return c.getAdjustedTypeWithFacts(t, TypeFactsNEUndefinedOrNull)
}
return t
}
right := expr.Right
rightType := c.getTypeOfExpression(right)
if !c.isTypeDerivedFrom(rightType, c.globalObjectType) {
return t
}
// if the right-hand side has an object type with a custom `[Symbol.hasInstance]` method, and that method
// has a type predicate, use the type predicate to perform narrowing. This allows normal `object` types to
// participate in `instanceof`, as per Step 2 of https://tc39.es/ecma262/#sec-instanceofoperator.
var predicate *TypePredicate
if signature := c.getEffectsSignature(expr.AsNode()); signature != nil {
predicate = c.getTypePredicateOfSignature(signature)
}
if predicate != nil && predicate.kind == TypePredicateKindIdentifier && predicate.parameterIndex == 0 {
return c.getNarrowedType(t, predicate.t, assumeTrue, true /*checkDerived*/)
}
if !c.isTypeDerivedFrom(rightType, c.globalFunctionType) {
return t
}
instanceType := c.mapType(rightType, c.getInstanceType)
// Don't narrow from `any` if the target type is exactly `Object` or `Function`, and narrow
// in the false branch only if the target is a non-empty object type.
if IsTypeAny(t) && (instanceType == c.globalObjectType || instanceType == c.globalFunctionType) || !assumeTrue && !(instanceType.flags&TypeFlagsObject != 0 && !c.isEmptyAnonymousObjectType(instanceType)) {
return t
}
return c.getNarrowedType(t, instanceType, assumeTrue, true /*checkDerived*/)
}
func (c *Checker) getNarrowedType(t *Type, candidate *Type, assumeTrue bool, checkDerived bool) *Type {
if t.flags&TypeFlagsUnion == 0 {
return c.getNarrowedTypeWorker(t, candidate, assumeTrue, checkDerived)
}
key := NarrowedTypeKey{t, candidate, assumeTrue, checkDerived}
if narrowedType, ok := c.narrowedTypes[key]; ok {
return narrowedType
}
narrowedType := c.getNarrowedTypeWorker(t, candidate, assumeTrue, checkDerived)
c.narrowedTypes[key] = narrowedType
return narrowedType
}
func (c *Checker) getNarrowedTypeWorker(t *Type, candidate *Type, assumeTrue bool, checkDerived bool) *Type {
if !assumeTrue {
if t == candidate {
return c.neverType
}
if checkDerived {
return c.filterType(t, func(t *Type) bool {
return !c.isTypeDerivedFrom(t, candidate)
})
}
trueType := c.getNarrowedType(t, candidate, true /*assumeTrue*/, false /*checkDerived*/)
return c.filterType(t, func(t *Type) bool {
return !c.isTypeSubsetOf(t, trueType)
})
}
if t.flags&TypeFlagsAnyOrUnknown != 0 {
return candidate
}
if t == candidate {
return candidate
}
// We first attempt to filter the current type, narrowing constituents as appropriate and removing
// constituents that are unrelated to the candidate.
var keyPropertyName string
if t.flags&TypeFlagsUnion != 0 {
keyPropertyName = c.getKeyPropertyName(t)
}
narrowedType := c.mapType(candidate, func(n *Type) *Type {
// If a discriminant property is available, use that to reduce the type.
matching := t
if keyPropertyName != "" {
if discriminant := c.getTypeOfPropertyOfType(n, keyPropertyName); discriminant != nil {
if constituent := c.getConstituentTypeForKeyType(t, discriminant); constituent != nil {
matching = constituent
}
}
}
// For each constituent t in the current type, if t and c are directly related, pick the most
// specific of the two. When t and c are related in both directions, we prefer c for type predicates
// because that is the asserted type, but t for `instanceof` because generics aren't reflected in
// prototype object types.
var mapType func(*Type) *Type
if checkDerived {
mapType = func(t *Type) *Type {
switch {
case c.isTypeDerivedFrom(t, n):
return t
case c.isTypeDerivedFrom(n, t):
return n
}
return c.neverType
}
} else {
mapType = func(t *Type) *Type {
switch {
case c.isTypeStrictSubtypeOf(t, n):
return t
case c.isTypeStrictSubtypeOf(n, t):
return n
case c.isTypeSubtypeOf(t, n):
return t
case c.isTypeSubtypeOf(n, t):
return n
}
return c.neverType
}
}
directlyRelated := c.mapType(matching, mapType)
if directlyRelated.flags&TypeFlagsNever == 0 {
return directlyRelated
}
// If no constituents are directly related, create intersections for any generic constituents that
// are related by constraint.
var isRelated func(*Type, *Type) bool
if checkDerived {
isRelated = c.isTypeDerivedFrom
} else {
isRelated = c.isTypeSubtypeOf
}
return c.mapType(t, func(t *Type) *Type {
if c.maybeTypeOfKind(t, TypeFlagsInstantiable) {
constraint := c.getBaseConstraintOfType(t)
if constraint == nil || isRelated(n, constraint) {
return c.getIntersectionType([]*Type{t, n})
}
}
return c.neverType
})
})
// If filtering produced a non-empty type, return that. Otherwise, pick the most specific of the two
// based on assignability, or as a last resort produce an intersection.
switch {
case narrowedType.flags&TypeFlagsNever == 0:
return narrowedType
case c.isTypeSubtypeOf(candidate, t):
return candidate
case c.isTypeAssignableTo(t, candidate):
return t
case c.isTypeAssignableTo(candidate, t):
return candidate
}
return c.getIntersectionType([]*Type{t, candidate})
}
func (c *Checker) getInstanceType(constructorType *Type) *Type {
prototypePropertyType := c.getTypeOfPropertyOfType(constructorType, "prototype")
if prototypePropertyType != nil && !IsTypeAny(prototypePropertyType) {
return prototypePropertyType
}
constructSignatures := c.getSignaturesOfType(constructorType, SignatureKindConstruct)
if len(constructSignatures) != 0 {
return c.getUnionType(core.Map(constructSignatures, func(signature *Signature) *Type {
return c.getReturnTypeOfSignature(c.getErasedSignature(signature))
}))
}
// We use the empty object type to indicate we don't know the type of objects created by
// this constructor function.
return c.emptyObjectType
}
func (c *Checker) narrowTypeByPrivateIdentifierInInExpression(f *FlowState, t *Type, expr *ast.BinaryExpression, assumeTrue bool) *Type {
target := c.getReferenceCandidate(expr.Right)
if !c.isMatchingReference(f.reference, target) {
return t
}
symbol := c.getSymbolForPrivateIdentifierExpression(expr.Left)
if symbol == nil {
return t
}
classSymbol := symbol.Parent
var targetType *Type
if ast.HasStaticModifier(symbol.ValueDeclaration) {
targetType = c.getTypeOfSymbol(classSymbol)
} else {
targetType = c.getDeclaredTypeOfSymbol(classSymbol)
}
return c.getNarrowedType(t, targetType, assumeTrue, true /*checkDerived*/)
}
func (c *Checker) narrowTypeByInKeyword(f *FlowState, t *Type, nameType *Type, assumeTrue bool) *Type {
name := getPropertyNameFromType(nameType)
isKnownProperty := someType(t, func(t *Type) bool {
return c.isTypePresencePossible(t, name, true /*assumeTrue*/)
})
if isKnownProperty {
// If the check is for a known property (i.e. a property declared in some constituent of
// the target type), we filter the target type by presence of absence of the property.
return c.filterType(t, func(t *Type) bool {
return c.isTypePresencePossible(t, name, assumeTrue)
})
}
if assumeTrue {
// If the check is for an unknown property, we intersect the target type with `Record<X, unknown>`,
// where X is the name of the property.
recordSymbol := c.getGlobalRecordSymbol()
if recordSymbol != nil {
return c.getIntersectionType([]*Type{t, c.getTypeAliasInstantiation(recordSymbol, []*Type{nameType, c.unknownType}, nil)})
}
}
return t
}
func (c *Checker) isTypePresencePossible(t *Type, propName string, assumeTrue bool) bool {
prop := c.getPropertyOfType(t, propName)
if prop != nil {
return prop.Flags&ast.SymbolFlagsOptional != 0 || prop.CheckFlags&ast.CheckFlagsPartial != 0 || assumeTrue
}