File: narrowingOfQualifiedNames.errors.txt

package info (click to toggle)
node-typescript 5.0.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 459,140 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (95 lines) | stat: -rw-r--r-- 3,102 bytes parent folder | download | duplicates (3)
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
tests/cases/compiler/narrowingOfQualifiedNames.ts(33,25): error TS2532: Object is possibly 'undefined'.
tests/cases/compiler/narrowingOfQualifiedNames.ts(38,29): error TS2532: Object is possibly 'undefined'.


==== tests/cases/compiler/narrowingOfQualifiedNames.ts (2 errors) ====
    // Repro from #43411
    
    interface IProperties {
        foo?: {
            aaa: string
            bbb: string
        }
    }
    
    function init(properties: IProperties) {
        if (properties.foo) {
            type FooOK = typeof properties.foo;
            properties.foo; // type is { aaa: string; bbb: string; }
            for (const x of [1, 2, 3]) {
              properties.foo; // type is { aaa: string; bbb: string; }
              type FooOrUndefined = typeof properties.foo; // type should be { aaa: string; bbb: string; }
            }
        }
    }
    
    interface DeepOptional {
        a?: {
            b?: {
                c?: string
            }
        }
    }
    
    function init2(foo: DeepOptional) {
        if (foo.a) {
            type A = typeof foo.a;
            type B = typeof foo.a.b;
            type C = typeof foo.a.b.c;
                            ~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
    
            for(const _ of [1]) {
                type A = typeof foo.a;
                type B = typeof foo.a.b;
                type C = typeof foo.a.b.c;
                                ~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
    
                if (foo.a.b) {
                    type A = typeof foo.a;
                    type B = typeof foo.a.b;
                    type C = typeof foo.a.b.c;
    
                    for(const _ of [1]) {
                        type A = typeof foo.a;
                        type B = typeof foo.a.b;
                        type C = typeof foo.a.b.c;
    
                        if (foo.a.b.c) {
                            type A = typeof foo.a;
                            type B = typeof foo.a.b;
                            type C = typeof foo.a.b.c;
    
                            for(const _ of [1]) {
                                type A = typeof foo.a;
                                type B = typeof foo.a.b;
                                type C = typeof foo.a.b.c;
                            }
                        }
                    }
                }
            }
        }
    }
    
    // Repro from #48289
    
    type Fish = { type: 'fish', hasFins: true }
    type Dog = { type: 'dog', saysWoof: true }
    
    type Pet = Fish | Dog;
    
    function handleDogBroken<PetType extends Pet>(pet: PetType) {
        if(pet.type === 'dog') {
            const _okay1 = pet.saysWoof;
            const _okay2: typeof pet.saysWoof = pet.saysWoof;
        }
    }
    
    function handleDogWorking(pet: Pet) {
        if(pet.type === 'dog') {
            const _okay1 = pet.saysWoof;
            const _okay2: typeof pet.saysWoof = pet.saysWoof;
        }
    }