File: typeGuardsWithInstanceOf.errors.txt

package info (click to toggle)
node-typescript 4.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 396,552 kB
  • sloc: javascript: 1,444,377; makefile: 7; sh: 3
file content (44 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download
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
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(7,20): error TS2339: Property 'global' does not exist on type 'never'.
  The intersection 'I & RegExp' was reduced to 'never' because property 'global' has conflicting types in some constituents.


==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts (1 errors) ====
    interface I { global: string; }
    var result!: I;
    var result2!: I;
    
    if (!(result instanceof RegExp)) {
        result = result2;
    } else if (!result.global) {
                       ~~~~~~
!!! error TS2339: Property 'global' does not exist on type 'never'.
!!! error TS2339:   The intersection 'I & RegExp' was reduced to 'never' because property 'global' has conflicting types in some constituents.
    }
    
    // Repro from #31155
    
    interface OnChanges {
        onChanges(changes: Record<string, unknown>): void
    }
    interface Validator {
        validate(): null | Record<string, unknown>;
    }
    
    class C {
        validate() {
            return {}
        }
    }
    
    function foo() {
        let v: Validator & Partial<OnChanges> = null as any;
        if (v instanceof C) {
            v // Validator & Partial<OnChanges> & C
        }
        v // Validator & Partial<OnChanges> via subtype reduction
        if (v.onChanges) {
            v.onChanges({});
        }
    }