forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsetTests.js
80 lines (70 loc) · 1.74 KB
/
subsetTests.js
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
//// [subsetTests.ts]
interface User {
name: string;
age: number;
contact: {
email: string;
phone: string;
address: {
street: string;
country: string;
zipcode: number;
}
};
password: string;
}
type PersonalInformation = Subset<User, {
name: string;
age: number;
}>; // Fine
const test: PersonalInformation = {
name: 'Hans',
age: 21,
password: 'string' // Error: password does not exist in type
};
type WronglyTypedPersonalInformation = Subset<User, {
name: string;
age: string; // Error: Types of property age are incompatible
}>;
type ExcessPersonalInformation = Subset<User, {
name: string;
favoriteColor: string; // Error: Property favoriteColor is missing in type User
}>;
// This also works for "deep" properties
type ShippingInformation = Subset<User, {
name: string;
contact: {
address: {
street: string;
zipcode: number;
}
}
}>; // Fine (Omitting properties of nested properties is ok too)
type WronglyTypedShippingInformation = Subset<User, {
name: string;
contact: {
address: {
street: {
name: string;
nr: number;
}; // Error: Types of property street are incompatible
zipcode: number;
}
}
}>;
type ExcessShippingInformation = Subset<User, {
name: string;
contact: {
address: {
street: string;
zipcode: number;
state: string; // Error: Property state is missing in type User
}
}
}>;
//// [subsetTests.js]
var test = {
name: 'Hans',
age: 21,
password: 'string' // Error: password does not exist in type
};