-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathindex-signatures.ts
165 lines (139 loc) · 2.98 KB
/
index-signatures.ts
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
161
162
163
164
165
module a {
let foo: any = {};
foo['Hello'] = 'World';
console.log(foo['Hello']); // World
}
module b {
class Foo {
constructor(public message: string) { };
log() {
console.log(this.message)
}
}
let foo: any = {};
foo['Hello'] = new Foo('World');
foo['Hello'].log(); // World
}
module c {
let obj = {
toString() {
console.log('toString called')
return 'Hello'
}
}
let foo: any = {};
foo[obj] = 'World'; // toString called
console.log(foo[obj]); // toString called, World
console.log(foo['Hello']); // World
}
module d {
let foo = ['World'];
console.log(foo[0]); // World
}
module e {
let obj = {
toString() {
return 'Hello'
}
}
let foo: any = {};
// ERROR: the index signature must be string, number ...
foo[obj] = 'World';
// FIX: TypeScript forces you to be explicit
foo[obj.toString()] = 'World';
}
module f {
let obj = { message: 'Hello' }
let foo: any = {};
// ERROR: the index signature must be string, number ...
foo[obj] = 'World';
// Here is what you actually stored!
console.log(foo["[object Object]"]); // World
}
module f {
console.log((1).toString()); // 1
console.log((2).toString()); // 2
}
module g {
let foo: { [index: string]: { message: string } } = {};
/**
* Must store stuff that conforms the structure
*/
/** Ok */
foo['a'] = { message: 'some message' };
/** Error: must contain a `message` or type string. You have a typo in `message` */
foo['a'] = { messages: 'some message' };
/**
* Stuff that is read is also type checked
*/
/** Ok */
foo['a'].message;
/** Error: messages does not exist. You have a typo in `message` */
foo['a'].messages;
}
module mustConform {
/** Okay */
interface Foo {
[key: string]: number
x: number;
y: number;
}
/** Error */
interface Bar {
[key: string]: number
x: number;
y: string; // Property `y` must of of type number
}
}
module mustConform2 {
interface Foo {
[key: string]: number
x: number;
}
let foo: Foo = { x: 1, y: 2 };
foo['x']; // number
let x = 'x'
foo[x]; // number
}
module dual {
interface ArrStr {
[key: string]: string | number; // Must accommodate all members
[index: number]: string; // Can be a subset of string indexer
// Just an example member
length: number;
}
}
module jsland {
interface NestedCSS {
color?: string;
[selector: string]: string | NestedCSS;
}
const example: NestedCSS = {
color: 'red',
'.subclass': {
color: 'blue'
}
}
const failsSilently: NestedCSS = {
colour: 'red', // No error as `colour` is a valid string selector
}
}
module better {
interface NestedCSS {
color?: string;
nest?: {
[selector: string]: NestedCSS;
}
}
const example: NestedCSS = {
color: 'red',
nest: {
'.subclass': {
color: 'blue'
}
}
}
const failsSilently: NestedCSS = {
colour: 'red', // TS Error: unknown property `colour`
}
}