File: objectLiteralExcessProperties.types

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (160 lines) | stat: -rw-r--r-- 4,398 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
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
=== tests/cases/compiler/objectLiteralExcessProperties.ts ===
interface Book {
    foreword: string;
>foreword : string
}

interface Cover {
    color?: string;
>color : string
}

var b1: Book = { forword: "oops" };
>b1 : Book
>{ forword: "oops" } : { forword: string; }
>forword : string
>"oops" : "oops"

var b2: Book | string = { foreward: "nope" };
>b2 : string | Book
>{ foreward: "nope" } : { foreward: string; }
>foreward : string
>"nope" : "nope"

var b3: Book | (Book[]) = [{ foreword: "hello" }, { forwards: "back" }];
>b3 : Book | Book[]
>[{ foreword: "hello" }, { forwards: "back" }] : ({ foreword: string; } | { forwards: string; })[]
>{ foreword: "hello" } : { foreword: string; }
>foreword : string
>"hello" : "hello"
>{ forwards: "back" } : { forwards: string; }
>forwards : string
>"back" : "back"

var b4: Book & Cover = { foreword: "hi", colour: "blue" };
>b4 : Book & Cover
>{ foreword: "hi", colour: "blue" } : { foreword: string; colour: string; }
>foreword : string
>"hi" : "hi"
>colour : string
>"blue" : "blue"

var b5: Book & Cover = { foreward: "hi", color: "blue" };
>b5 : Book & Cover
>{ foreward: "hi", color: "blue" } : { foreward: string; color: string; }
>foreward : string
>"hi" : "hi"
>color : string
>"blue" : "blue"

var b6: Book & Cover = { foreword: "hi", color: "blue", price: 10.99 };
>b6 : Book & Cover
>{ foreword: "hi", color: "blue", price: 10.99 } : { foreword: string; color: string; price: number; }
>foreword : string
>"hi" : "hi"
>color : string
>"blue" : "blue"
>price : number
>10.99 : 10.99

var b7: Book & number = { foreword: "hi", price: 10.99 };
>b7 : Book & number
>{ foreword: "hi", price: 10.99 } : { foreword: string; price: number; }
>foreword : string
>"hi" : "hi"
>price : number
>10.99 : 10.99

var b8: Cover | Cover[] = { couleur : "non" };
>b8 : Cover | Cover[]
>{ couleur : "non" } : { couleur: string; }
>couleur : string
>"non" : "non"

var b9: Book | Book[] = { forewarned: "still no" };
>b9 : Book | Book[]
>{ forewarned: "still no" } : { forewarned: string; }
>forewarned : string
>"still no" : "still no"

interface Indexed {
    [n: number]: Cover;
>n : number
}

var b10: Indexed = { 0: { }, '1': { } }; // ok
>b10 : Indexed
>{ 0: { }, '1': { } } : { 0: {}; '1': {}; }
>0 : {}
>{ } : {}
>'1' : {}
>{ } : {}

var b11: Indexed = { 0: { colour: "blue" } }; // nested object literal still errors
>b11 : Indexed
>{ 0: { colour: "blue" } } : { 0: { colour: string; }; }
>0 : { colour: string; }
>{ colour: "blue" } : { colour: string; }
>colour : string
>"blue" : "blue"

// Repros inspired by #28752

function test<T extends IFoo>() {
>test : <T extends any>() => void

    // No excess property checks on generic types
    const obj1: T = { name: "test" };
>obj1 : T
>{ name: "test" } : { name: string; }
>name : string
>"test" : "test"

    // No excess property checks on intersections involving generics
    const obj2: T & { prop: boolean } = { name: "test", prop: true };
>obj2 : T & { prop: boolean; }
>prop : boolean
>{ name: "test", prop: true } : { name: string; prop: boolean; }
>name : string
>"test" : "test"
>prop : boolean
>true : true

    // Excess property checks only on non-generic parts of unions
    const obj3: T | { prop: boolean } = { name: "test", prop: true };
>obj3 : T | { prop: boolean; }
>prop : boolean
>{ name: "test", prop: true } : { name: string; prop: boolean; }
>name : string
>"test" : "test"
>prop : boolean
>true : true

    // Excess property checks only on non-generic parts of unions
    const obj4: T & { prop: boolean } | { name: string } = { name: "test", prop: true };
>obj4 : { name: string; } | (T & { prop: boolean; })
>prop : boolean
>name : string
>{ name: "test", prop: true } : { name: string; prop: boolean; }
>name : string
>"test" : "test"
>prop : boolean
>true : true

    // No excess property checks when union includes 'object' type
    const obj5: object | { x: string } = { z: 'abc' }
>obj5 : object | { x: string; }
>x : string
>{ z: 'abc' } : { z: string; }
>z : string
>'abc' : "abc"

    // The 'object' type has no effect on intersections
    const obj6: object & { x: string } = { z: 'abc' }
>obj6 : object & { x: string; }
>x : string
>{ z: 'abc' } : { z: string; }
>z : string
>'abc' : "abc"
}