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
#![allow(clippy::if_same_then_else)]

use std::{borrow::Cow, mem::take, ops::AddAssign};

use rnode::{Fold, FoldWith, Visit, VisitWith};
use stc_ts_ast_rnode::{RBreakStmt, RIdent, RReturnStmt, RStmt, RStr, RThrowStmt, RTsEntityName, RTsLit, RYieldExpr};
use stc_ts_errors::{DebugExt, ErrorKind};
use stc_ts_simple_ast_validations::yield_check::YieldValueUsageFinder;
use stc_ts_types::{
    CommonTypeMetadata, Index, IndexedAccessType, Instance, Key, KeywordType, KeywordTypeMetadata, LitType, MethodSignature,
    PropertySignature, Ref, RefMetadata, TypeElement, TypeParamInstantiation,
};
use stc_utils::{
    cache::Freeze,
    dev_span,
    ext::{SpanExt, TypeVecExt},
};
use swc_common::{Span, Spanned, SyntaxContext, TypeEq, DUMMY_SP};
use swc_ecma_ast::*;
use tracing::debug;

use crate::{
    analyzer::{
        assign::AssignOpts,
        expr::{GetIteratorOpts, TypeOfMode},
        scope::ExpandOpts,
        util::ResultExt,
        Analyzer, Ctx,
    },
    ty::{Array, Type, TypeExt},
    validator,
    validator::ValidateWith,
    VResult,
};

#[derive(Debug, Default)]
pub(in crate::analyzer) struct ReturnValues {
    /// If all cases are handled, `return literal` like `return 5` and never
    /// type is used, we should not generalize the return value.
    should_generalize: bool,
    pub return_types: Vec<Type>,
    yield_types: Vec<Type>,
    /// Are we in if or switch statement?
    pub(super) in_conditional: bool,
}

impl AddAssign for ReturnValues {
    #[allow(clippy::suspicious_op_assign_impl)]
    fn add_assign(&mut self, rhs: Self) {
        self.should_generalize |= rhs.should_generalize;

        self.return_types.extend(rhs.return_types);
        self.yield_types.extend(rhs.yield_types);
    }
}

impl Analyzer<'_, '_> {
    /// This method returns `Generator` if `yield` is found.
    pub(in crate::analyzer) fn visit_stmts_for_return(
        &mut self,
        span: Span,
        is_async: bool,
        is_generator: bool,
        stmts: &Vec<RStmt>,
    ) -> VResult<Option<Type>> {
        let _tracing = dev_span!("visit_stmts_for_return");

        let marks = self.marks();

        debug_assert_eq!(span.ctxt, SyntaxContext::empty());
        debug!("visit_stmts_for_return()");
        debug_assert!(!self.config.is_builtin, "builtin: visit_stmts_for_return should not be called");

        let mut unconditional_throw = None;
        for stmt in stmts {
            if let RStmt::Throw(throws) = stmt {
                unconditional_throw = Some(throws.span);
                break;
            }
        }

        let cannot_fallback_to_iterable_iterator = self.rule().strict_null_checks && {
            let mut v = YieldValueUsageFinder::default();

            stmts.visit_with(&mut v);

            v.found
        };

        // let mut old_ret_tys = self.scope.return_types.take();

        let mut is_unreachable = false;
        let mut ret_ty = (|| -> VResult<_> {
            let mut values: ReturnValues = {
                let ctx = Ctx {
                    cannot_fallback_to_iterable_iterator,
                    ..self.ctx
                };
                self.with_ctx(ctx).with(|analyzer: &mut Analyzer| {
                    analyzer.validate_stmts_and_collect(&stmts.iter().collect::<Vec<_>>());
                    is_unreachable = analyzer.ctx.in_unreachable;
                    take(&mut analyzer.scope.return_values)
                })
            };

            {
                //  Expand return types if no element references a type parameter
                let can_expand = !values.return_types.iter().any(should_preserve_ref);

                if can_expand {
                    values.return_types = values
                        .return_types
                        .into_iter()
                        .map(|ty| {
                            debug_assert_ne!(ty.span(), DUMMY_SP);

                            self.expand(
                                ty.span(),
                                ty,
                                ExpandOpts {
                                    full: true,
                                    expand_union: true,
                                    preserve_ref: true,
                                    ..Default::default()
                                },
                            )
                        })
                        .collect::<Result<_, _>>()
                        .report(&mut self.storage)
                        .unwrap_or_default();

                    values.yield_types = values
                        .yield_types
                        .into_iter()
                        .map(|ty| {
                            self.expand(
                                ty.span(),
                                ty,
                                ExpandOpts {
                                    full: true,
                                    expand_union: true,
                                    ..Default::default()
                                },
                            )
                        })
                        .collect::<Result<_, _>>()
                        .report(&mut self.storage)
                        .unwrap_or_default();
                }
            }

            {
                if let Some(span) = unconditional_throw {
                    values.return_types.push(Type::never(span, Default::default()));
                }
            }

            debug!("visit_stmts_for_return: types.len() = {}", values.return_types.len());

            let mut actual = Vec::with_capacity(values.return_types.len());
            for mut ty in values.return_types {
                ty = ty.fold_with(&mut KeyInliner { analyzer: self });
                if values.should_generalize {
                    ty = ty.generalize_lit();
                }

                actual.push(ty);
            }

            if is_generator {
                let mut types = Vec::with_capacity(values.yield_types.len());

                let is_all_null_or_undefined = values.yield_types.iter().all(|ty| ty.is_null_or_undefined());

                for ty in values.yield_types {
                    let ty = self.simplify(ty);
                    types.push(ty);
                }

                if is_all_null_or_undefined {
                    types.clear();
                }

                if types.is_empty() {
                    if let Some(declared) = self.scope.declared_return_type().cloned() {
                        // TODO(kdy1): Change this to `get_iterable_element_type`
                        if let Ok(el_ty) = self.get_iterator_element_type(span, Cow::Owned(declared), true, Default::default()) {
                            types.push(el_ty.into_owned());
                        }
                    }
                }

                let yield_ty = if types.is_empty() {
                    Type::any(
                        DUMMY_SP,
                        KeywordTypeMetadata {
                            common: CommonTypeMetadata {
                                implicit: true,
                                ..Default::default()
                            },
                            ..Default::default()
                        },
                    )
                } else {
                    Type::new_union(span, types)
                };

                let ret_ty = if actual.is_empty() {
                    Type::void(span, Default::default())
                } else {
                    self.simplify(Type::new_union(span, actual))
                };

                let mut metadata = yield_ty.metadata();

                return Ok(Some(Type::Ref(Ref {
                    span: yield_ty.span().or_else(|| {
                        metadata = ret_ty.metadata();
                        ret_ty.span()
                    }),
                    type_name: if is_async {
                        RTsEntityName::Ident(RIdent::new("AsyncGenerator".into(), DUMMY_SP))
                    } else {
                        if cannot_fallback_to_iterable_iterator || self.env.get_global_type(span, &"Generator".into()).is_ok() {
                            RTsEntityName::Ident(RIdent::new("Generator".into(), DUMMY_SP))
                        } else {
                            RTsEntityName::Ident(RIdent::new("IterableIterator".into(), DUMMY_SP))
                        }
                    },
                    type_args: Some(box TypeParamInstantiation {
                        span,
                        params: vec![
                            yield_ty,
                            ret_ty,
                            Type::Keyword(KeywordType {
                                span,
                                kind: TsKeywordTypeKind::TsUnknownKeyword,
                                metadata: Default::default(),
                                tracker: Default::default(),
                            }),
                        ],
                    }),
                    metadata: RefMetadata {
                        common: metadata,
                        ..Default::default()
                    },
                    tracker: Default::default(),
                })));
            }

            if is_async {
                let ret_ty = if actual.is_empty() {
                    Type::void(span, Default::default())
                } else {
                    self.simplify(Type::new_union(span, actual))
                };

                return Ok(Some(Type::Ref(Ref {
                    span,
                    type_name: RTsEntityName::Ident(RIdent::new("Promise".into(), DUMMY_SP)),
                    type_args: Some(box TypeParamInstantiation {
                        span,
                        params: vec![ret_ty],
                    }),
                    metadata: Default::default(),
                    tracker: Default::default(),
                })));
            }

            let is_all_null_or_undefined = actual.iter().all(|ty| ty.is_null_or_undefined());

            if !actual.is_empty() && is_all_null_or_undefined {
                return Ok(Some(Type::any(span, Default::default())));
            }

            if actual.is_empty() {
                return Ok(None);
            }

            actual.dedup_type();

            if actual.len() == 1 {
                return Ok(actual.pop());
            }

            let ty = Type::new_union(span, actual);
            let ty = self.simplify(ty);

            // print_type("Return",  &ty);

            Ok(Some(ty))
        })()?;
        ret_ty.freeze();

        if self.config.is_builtin {
            return Ok(ret_ty);
        }

        if let Some(declared) = self.scope.declared_return_type().cloned() {
            if !is_async && !is_generator {
                if ret_ty.is_none() && !is_unreachable {
                    if let Type::Keyword(KeywordType {
                        kind: TsKeywordTypeKind::TsNeverKeyword,
                        span,
                        ..
                    }) = declared
                    {
                        self.storage.report(ErrorKind::CannotFunctionReturningNever { span }.into());
                    }
                }
                // Noop
            } else if is_generator && declared.is_kwd(TsKeywordTypeKind::TsVoidKeyword) {
                // We use different error code
            } else if let Some(ret_ty) = &ret_ty {
                let declared = Type::Instance(Instance {
                    span: declared.span(),
                    ty: box declared,
                    metadata: Default::default(),
                    tracker: Default::default(),
                });

                let ret_ty = Type::Instance(Instance {
                    span: ret_ty.span(),
                    ty: box ret_ty.clone(),
                    metadata: Default::default(),
                    tracker: Default::default(),
                });

                self.assign_with_opts(
                    &mut Default::default(),
                    &declared,
                    &ret_ty,
                    AssignOpts {
                        span,
                        allow_unknown_rhs: Some(true),
                        may_unwrap_promise: is_async,
                        ..Default::default()
                    },
                )
                .context("tried to assign return type")
                .report(&mut self.storage);
            }
        }

        Ok(ret_ty)
    }
}

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, node: &RReturnStmt) {
        debug_assert!(!self.config.is_builtin, "builtin: return statement is not supported");
        debug_assert_ne!(node.span, DUMMY_SP, "return statement should have valid span");

        let mut ty = if let Some(res) = {
            let ctx = Ctx {
                in_return_arg: true,
                ..self.ctx
            };
            let mut a = self.with_ctx(ctx);

            let type_ann = a.scope.declared_return_type().cloned();
            node.arg.validate_with_args(&mut *a, (TypeOfMode::RValue, None, type_ann.as_ref()))
        } {
            res?
        } else {
            Type::Keyword(KeywordType {
                span: node.span,
                kind: TsKeywordTypeKind::TsVoidKeyword,
                metadata: Default::default(),
                tracker: Default::default(),
            })
        };
        debug_assert_ne!(ty.span(), DUMMY_SP, "{:?}", ty);
        ty.freeze();

        if let Some(declared) = self.scope.declared_return_type().cloned() {
            let declared = Type::Instance(Instance {
                span: declared.span(),
                ty: box declared,
                metadata: Default::default(),
                tracker: Default::default(),
            });

            match (self.ctx.in_async, self.ctx.in_generator) {
                // AsyncGenerator
                (true, true) => {
                    self.assign_with_opts(
                        &mut Default::default(),
                        &declared,
                        &Type::Ref(Ref {
                            span: node.span,
                            type_name: RTsEntityName::Ident(RIdent::new("AsyncGenerator".into(), node.span)),
                            type_args: Some(box TypeParamInstantiation {
                                span: node.span,
                                params: vec![Type::any(DUMMY_SP, Default::default()), ty.clone()],
                            }),
                            metadata: Default::default(),
                            tracker: Default::default(),
                        }),
                        AssignOpts {
                            span: node.span,
                            allow_unknown_rhs: Some(true),
                            allow_assignment_of_void: Some(!self.rule().strict_null_checks),
                            may_unwrap_promise: true,
                            ..Default::default()
                        },
                    )
                    .context("tried to validate the return type of an async generator")
                    .report(&mut self.storage);
                }

                // Promise
                (true, false) => {
                    self.assign_with_opts(
                        &mut Default::default(),
                        &declared,
                        &ty,
                        AssignOpts {
                            span: node.span,
                            allow_unknown_rhs: Some(true),
                            allow_assignment_of_void: Some(!self.rule().strict_null_checks),
                            may_unwrap_promise: true,
                            ..Default::default()
                        },
                    )
                    .context("tried to validate the return type of an async function")
                    .report(&mut self.storage);
                }

                // Generator
                (false, true) => {
                    let name = if self.ctx.cannot_fallback_to_iterable_iterator
                        || self.env.get_global_type(node.span, &"Generator".into()).is_ok()
                    {
                        "Generator"
                    } else {
                        "IterableIterator"
                    };

                    self.assign_with_opts(
                        &mut Default::default(),
                        &declared,
                        &Type::Ref(Ref {
                            span: node.span,
                            type_name: RTsEntityName::Ident(RIdent::new(name.into(), node.span)),
                            type_args: Some(box TypeParamInstantiation {
                                span: node.span,
                                params: vec![Type::any(DUMMY_SP, Default::default()), ty.clone()],
                            }),
                            metadata: Default::default(),
                            tracker: Default::default(),
                        }),
                        AssignOpts {
                            span: node.span,
                            allow_unknown_rhs: Some(true),
                            allow_assignment_of_void: Some(!self.rule().strict_null_checks),
                            ..Default::default()
                        },
                    )
                    .context("tried to validate the return type of a generator")
                    .report(&mut self.storage);
                }

                (false, false) => {
                    self.assign_with_opts(
                        &mut Default::default(),
                        &declared,
                        &ty,
                        AssignOpts {
                            span: node.span,
                            allow_unknown_rhs: Some(true),
                            allow_assignment_of_void: Some(!self.rule().strict_null_checks),

                            ..Default::default()
                        },
                    )
                    .context("tried to validate the return type of a function")
                    .report(&mut self.storage);
                }
            }
        }

        self.scope.return_values.return_types.push(ty);

        Ok(())
    }
}

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, e: &RYieldExpr) -> VResult<Type> {
        let span = e.span;

        if let Some(res) = e.arg.validate_with_default(self) {
            let ty = res?;

            let item_ty = if e.delegate {
                if self.ctx.in_async {
                    self.get_async_iterator_element_type(e.span, Cow::Owned(ty))
                        .context("tried to convert argument as an async iterator for delegating yield")?
                        .into_owned()
                } else {
                    self.get_iterator_element_type(e.span, Cow::Owned(ty), false, GetIteratorOpts { ..Default::default() })
                        .context("tried to convert argument as an iterator for delegating yield")?
                        .into_owned()
                }
            } else {
                ty
            }
            .freezed();

            if let Some(declared) = self.scope.declared_return_type().cloned() {
                match if self.ctx.in_async {
                    self.get_async_iterator_element_type(e.span, Cow::Owned(declared))
                        .context("tried to get an element type from an async iterator for normal yield")
                } else {
                    self.get_iterator_element_type(e.span, Cow::Owned(declared), true, GetIteratorOpts { ..Default::default() })
                        .context("tried to get an element type from an iterator for normal yield")
                }
                .map(Cow::into_owned)
                .map(Freeze::freezed)
                {
                    Ok(declared) => {
                        let declared = Type::Instance(Instance {
                            span: declared.span(),
                            ty: box declared,
                            metadata: Default::default(),
                            tracker: Default::default(),
                        });

                        match self.assign_with_opts(
                            &mut Default::default(),
                            &declared,
                            &item_ty,
                            AssignOpts {
                                span: e.span,
                                allow_unknown_rhs: Some(true),
                                use_missing_fields_for_class: true,
                                ..Default::default()
                            },
                        ) {
                            Ok(()) => {}
                            Err(err) => {
                                self.storage.report(err);
                                return Ok(Type::any(span, Default::default()));
                            }
                        }
                    }
                    Err(err) => {
                        self.storage.report(
                            ErrorKind::SimpleAssignFailed {
                                span,
                                cause: Some(box err),
                            }
                            .into(),
                        );
                        return Ok(Type::any(span, Default::default()));
                    }
                }
            }

            self.scope.return_values.yield_types.push(item_ty);
        } else {
            self.scope.return_values.yield_types.push(Type::Keyword(KeywordType {
                span: e.span,
                kind: TsKeywordTypeKind::TsUndefinedKeyword,
                metadata: Default::default(),
                tracker: Default::default(),
            }));
        }

        Ok(Type::any(e.span, Default::default()))
    }
}

pub(super) struct LoopBreakerFinder {
    pub found: bool,
}

impl Visit<RBreakStmt> for LoopBreakerFinder {
    fn visit(&mut self, _: &RBreakStmt) {
        self.found = true;
    }
}

impl Visit<RThrowStmt> for LoopBreakerFinder {
    fn visit(&mut self, _: &RThrowStmt) {
        self.found = true;
    }
}

impl Visit<RReturnStmt> for LoopBreakerFinder {
    fn visit(&mut self, _: &RReturnStmt) {
        self.found = true;
    }
}

fn should_preserve_ref(ty: &Type) -> bool {
    match ty {
        Type::IndexedAccessType(..) => true,
        Type::Array(Array { elem_type, .. }) => should_preserve_ref(elem_type),
        // TODO(kdy1): More work
        _ => false,
    }
}

/// # Example
///
/// ```ts
/// declare type S2 = {
///     a: string;
///     b: string;
/// };
/// T[keyof S2];
///
/// // becomes
///
/// T["a" | "b"]
/// ```
struct KeyInliner<'a, 'b, 'c> {
    analyzer: &'a mut Analyzer<'b, 'c>,
}

impl Fold<Type> for KeyInliner<'_, '_, '_> {
    fn fold(&mut self, mut ty: Type) -> Type {
        // TODO(kdy1): PERF
        ty.normalize_mut();

        ty = ty.fold_children_with(self);

        if let Type::IndexedAccessType(IndexedAccessType {
            span,
            readonly,
            ref obj_type,
            index_type:
                box Type::Index(Index {
                    span: op_span,
                    ty: ref index_type,
                    metadata: op_metadata,
                    ..
                }),
            metadata,
            ..
        }) = ty
        {
            // TODO(kdy1): Handle error.
            let index_ty = self
                .analyzer
                .expand(
                    span,
                    *index_type.clone(),
                    ExpandOpts {
                        full: true,
                        expand_union: true,
                        preserve_ref: false,
                        ignore_expand_prevention_for_top: true,
                        ..Default::default()
                    },
                )
                .unwrap_or_else(|_| *index_type.clone());

            if obj_type.type_eq(index_type) {
                // declare type S2 = {
                //     a: string;
                //     b: string;
                // };
                // `S2[keyof S2]`;

                // TODO(kdy1): PERF
                if let Type::TypeLit(obj) = index_ty.foldable() {
                    let mut types: Vec<Type> = vec![];
                    for member in obj.members {
                        match member {
                            TypeElement::Call(_) => {
                                unimplemented!("Call signature in S[keyof S]")
                            }
                            TypeElement::Constructor(_) => {
                                unimplemented!("Constructor signature in S[keyof S]")
                            }
                            TypeElement::Property(p) => {
                                if p.key.is_computed() {
                                    unimplemented!("Computed key mixed with S[keyof S]")
                                }

                                if let Some(ty) = p.type_ann {
                                    if types.iter().all(|previous| !previous.type_eq(&ty)) {
                                        types.push(*ty);
                                    }
                                }
                            }
                            TypeElement::Method(_) => {
                                unimplemented!("Method property in S[keyof S]")
                            }
                            TypeElement::Index(_) => {
                                unimplemented!("Index signature in S[keyof S]")
                            }
                        }
                    }
                    let ty = Type::new_union(span, types);

                    return ty;
                }
            } else {
                // declare type S2 = {
                //     a: string;
                //     b: string;
                // };
                // `T[keyof S2]`;
                // =>
                // `T["a" | "b"]`

                // TODO(kdy1): PERF
                if let Some(obj) = index_ty.type_lit() {
                    let mut types: Vec<Type> = vec![];
                    for member in obj.members {
                        match member {
                            TypeElement::Call(_) => {
                                unimplemented!("Call signature in T[keyof S]")
                            }
                            TypeElement::Constructor(_) => {
                                unimplemented!("Constructor signature in T[keyof S]")
                            }

                            TypeElement::Index(_) => {
                                unimplemented!("Index signature in T[keyof S]")
                            }

                            TypeElement::Property(PropertySignature { key, .. }) | TypeElement::Method(MethodSignature { key, .. }) => {
                                if key.is_computed() {
                                    unimplemented!("Computed key mixed with T[keyof S]");
                                }

                                if let Key::Normal { span: i_span, sym: key } = key {
                                    debug_assert_eq!(i_span.ctxt, SyntaxContext::empty());
                                    let ty = Type::Lit(LitType {
                                        span: i_span,
                                        lit: RTsLit::Str(RStr {
                                            span: i_span,
                                            value: key.clone(),
                                            raw: None,
                                        }),
                                        metadata: Default::default(),
                                        tracker: Default::default(),
                                    });

                                    if types.iter().all(|previous| !previous.type_eq(&ty)) {
                                        types.push(ty);
                                    }
                                }
                            }
                        }
                    }
                    return Type::IndexedAccessType(IndexedAccessType {
                        span,
                        readonly,
                        obj_type: obj_type.clone(),
                        index_type: box Type::new_union(span, types),
                        metadata,
                        tracker: Default::default(),
                    });
                }
            }
        }

        ty
    }
}