-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathresearch-spec.ts
214 lines (193 loc) · 8.13 KB
/
research-spec.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @license Copyright © 2017 Nicholas Jamieson. All Rights Reserved.
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/cartant/ts-action
*/
/*tslint:disable:no-unused-expression*/
import { expect } from "chai";
import { Compiler, snippet } from "ts-snippet";
describe("research", function (): void {
/*tslint:disable-next-line:no-invalid-this*/
this.timeout(5000);
const compiler = new Compiler({
moduleResolution: "node",
target: "es2015"
});
const expectSnippet = (code: string) => snippet({
"research.ts": `
import { action, empty, payload } from "./source";
${code}
`
}, compiler).expect("research.ts");
describe("narrowing", () => {
it("should not narrow with non-const, implicit string-literal types", () => {
expectSnippet(`
const A_NAME = "[research] A";
const B_NAME = "[research] B";
let A_TYPE = A_NAME;
let B_TYPE = B_NAME;
export class A {
static readonly type = A_TYPE;
readonly type = A.type;
constructor(public payload: { a: number } = { a: 0 }) {}
}
export class B {
static readonly type = B_TYPE;
readonly type = B.type;
constructor() {}
}
const narrow = (action: A | B) => {
if (action.type === A_TYPE) {
console.log(action.type, action.payload.a);
} else {
console.log(action.type);
}
};
`).toFail(/'payload' does not exist on type/);
});
it("should narrow with non-const, explicit string-literal types", () => {
expectSnippet(`
const A_NAME: "[research] A" = "[research] A";
const B_NAME: "[research] B" = "[research] B";
let A_TYPE = A_NAME;
let B_TYPE = B_NAME;
export class A {
static readonly type = A_TYPE;
readonly type = A.type;
constructor(public payload: { a: number } = { a: 0 }) {}
}
export class B {
static readonly type = B_TYPE;
readonly type = B.type;
constructor() {}
}
const narrow = (action: A | B) => {
if (action.type === A_TYPE) {
console.log(action.type, action.payload.a);
} else {
console.log(action.type);
}
};
`).toSucceed();
});
it("should narrow declared actions", () => {
expectSnippet(`
export class A {
static readonly type = "[research] A";
readonly type = A.type;
constructor(public payload: { a: number } = { a: 0 }) {}
}
export class B {
static readonly type = "[research] B";
readonly type = B.type;
constructor() {}
}
const narrow = (action: A | B) => {
if (action.type === A.type) {
console.log(action.type, action.payload.a);
} else {
console.log(action.type);
}
};
`).toSucceed();
});
it("should narrow declared actions using typeof", () => {
expectSnippet(`
export class A {
static readonly type = "[research] A";
readonly type = A.type;
constructor(public payload: { a: number } = { a: 0 }) {}
}
export class B {
static readonly type = "[research] B";
readonly type = B.type;
constructor() {}
}
const a: A = undefined!;
const b: B = undefined!;
const narrow = (action: typeof a | typeof b) => {
if (action.type === A.type) {
console.log(action.type, action.payload.a);
} else {
console.log(action.type);
}
};
`).toSucceed();
});
it("should narrow actions using 'type'", () => {
expectSnippet(`
const A = action({ type: "[research] A", ...payload<{ a: number }>() });
const B = action({ type: "[research] B", ...empty() });
const narrow = (action: typeof A.action | typeof B.action) => {
if (action.type === A.type) {
console.log(action.type, action.payload.a);
} else {
console.log(action.type);
}
};
`).toSucceed();
});
it("should narrow actions using 'action.type'", () => {
expectSnippet(`
const A = action({ type: "[research] A", ...payload<{ a: number }>() });
const B = action({ type: "[research] B", ...empty() });
const narrow = (action: typeof A.action | typeof B.action) => {
if (action.type === A.action.type) {
console.log(action.type, action.payload.a);
} else {
console.log(action.type);
}
};
`).toSucceed();
});
});
describe("widening", () => {
describe.skip("likely bugs", () => {
it("should widen a mutated parameter", () => {
expectSnippet(`
const get = <T, N>(t: T, name: N) => { name = "bob"; return name; };
const result = get(42, "alice");
`).toInfer("result", "string");
});
it("should use the default if type parameters are partially specified", () => {
expectSnippet(`
const get = <T, N extends string = string>(t: T, name: N) => name;
const result = get<number>(42, "alice");
`).toInfer("result", "string");
});
});
it("should not widen a readonly interface member", () => {
expectSnippet(`
const get = <T, N extends string>(options: { readonly name: N, t: T }) => options.name;
const result = get({ name: "alice", t: 42 });
`).toInfer("result", `"alice"`);
});
it("should infer {} if type parameters are partially specified", () => {
expectSnippet(`
const get = <T, N>(t: T, name: N) => name;
const result = get<number>(42, "alice");
`).toInfer("result", "{}");
});
it("should infer {} if type parameters are partially specified", () => {
expectSnippet(`
const get = <T, N extends string>(options: { readonly name: N, t: T }) => options.name;
const result = get<number>({ name: "alice", t: 42 });
`).toInfer("result", "{}");
});
it("should use the default if type parameters are partially specified", () => {
expectSnippet(`
const get = <T, N extends string = string>(options: { readonly name: N, t: T }) => options.name;
const result = get<number>({ name: "alice", t: 42 });
`).toInfer("result", "string");
});
});
describe("payload defaults", () => {
it("should not allow literal defaults", () => {
expectSnippet(`
const maker = <T>() => (t: T) => t;
const make = maker<{ a: number = 42, b: number = 56 }>();
const made = make({});
`).toFail(/type literal property cannot have an initializer/);
});
});
});