File: excessPropertyCheckWithMultipleDiscriminants.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 (78 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download | duplicates (4)
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
tests/cases/compiler/excessPropertyCheckWithMultipleDiscriminants.ts(30,5): error TS2322: Type '{ type: "number"; value: number; multipleOf: number; format: string; }' is not assignable to type 'Primitive'.
  Object literal may only specify known properties, and 'multipleOf' does not exist in type 'Float'.
tests/cases/compiler/excessPropertyCheckWithMultipleDiscriminants.ts(41,5): error TS2322: Type '{ p1: "left"; p2: false; p3: number; p4: string; }' is not assignable to type 'DisjointDiscriminants'.
  Object literal may only specify known properties, and 'p3' does not exist in type '{ p1: "left"; p2: boolean; }'.
tests/cases/compiler/excessPropertyCheckWithMultipleDiscriminants.ts(57,5): error TS2322: Type '{ p1: "right"; p2: false; p3: number; p4: string; }' is not assignable to type 'DisjointDiscriminants'.
  Object literal may only specify known properties, and 'p3' does not exist in type '{ p1: "right"; p2: false; p4: string; }'.


==== tests/cases/compiler/excessPropertyCheckWithMultipleDiscriminants.ts (3 errors) ====
    // Repro from #32657
    
    interface Base<T> {
        value: T;
    }
    
    interface Int extends Base<number> {
        type: "integer";
        multipleOf?: number;
    }
    
    interface Float extends Base<number> {
        type: "number";
    }
    
    interface Str extends Base<string> {
        type: "string";
        format?: string;
    }
    
    interface Bool extends Base<boolean> {
        type: "boolean";
    }
    
    type Primitive = Int | Float | Str | Bool;
    
    const foo: Primitive = {
        type: "number",
        value: 10,
        multipleOf: 5, // excess property
        ~~~~~~~~~~~~~
!!! error TS2322: Type '{ type: "number"; value: number; multipleOf: number; format: string; }' is not assignable to type 'Primitive'.
!!! error TS2322:   Object literal may only specify known properties, and 'multipleOf' does not exist in type 'Float'.
        format: "what?"
    }
    
    
    type DisjointDiscriminants = { p1: 'left'; p2: true; p3: number } | { p1: 'right'; p2: false; p4: string } | { p1: 'left'; p2: boolean };
    
    // This has excess error because variant three is the only applicable case.
    const a: DisjointDiscriminants = {
        p1: 'left',
        p2: false,
        p3: 42,
        ~~~~~~
!!! error TS2322: Type '{ p1: "left"; p2: false; p3: number; p4: string; }' is not assignable to type 'DisjointDiscriminants'.
!!! error TS2322:   Object literal may only specify known properties, and 'p3' does not exist in type '{ p1: "left"; p2: boolean; }'.
        p4: "hello"
    };
    
    // This has no excess error because variant one and three are both applicable.
    const b: DisjointDiscriminants = {
        p1: 'left',
        p2: true,
        p3: 42,
        p4: "hello"
    };
    
    // This has excess error because variant two is the only applicable case
    const c: DisjointDiscriminants = {
        p1: 'right',
        p2: false,
        p3: 42,
        ~~~~~~
!!! error TS2322: Type '{ p1: "right"; p2: false; p3: number; p4: string; }' is not assignable to type 'DisjointDiscriminants'.
!!! error TS2322:   Object literal may only specify known properties, and 'p3' does not exist in type '{ p1: "right"; p2: false; p4: string; }'.
        p4: "hello"
    };