Skip to content

Commit 28fae65

Browse files
committed
test: provide a static type checking against example usages
1 parent cb908ca commit 28fae65

File tree

4 files changed

+640
-0
lines changed

4 files changed

+640
-0
lines changed

test/typecheck.action.ts

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* *** MIT LICENSE ***
3+
* -------------------------------------------------------------------------
4+
* This code may be modified and distributed under the MIT license.
5+
* See the LICENSE file for details.
6+
* -------------------------------------------------------------------------
7+
*
8+
* @summary A test for the typescript definition
9+
*
10+
* @author Alvis HT Tang <[email protected]>
11+
* @license MIT
12+
* @copyright Copyright (c) 2018 - All Rights Reserved.
13+
* -------------------------------------------------------------------------
14+
*/
15+
16+
import { Action, ErroneousAction, StandardAction } from '../definitions';
17+
18+
//
19+
// Action Type
20+
//
21+
22+
const TEST_TYPE = 'type';
23+
type TestType = typeof TEST_TYPE;
24+
25+
interface TestPayload {
26+
data: 'payload';
27+
}
28+
29+
interface TestMeta {
30+
data: 'meta';
31+
}
32+
33+
{
34+
const action: StandardAction = {
35+
type: TEST_TYPE
36+
};
37+
}
38+
39+
{
40+
const action: ErroneousAction = {
41+
type: TEST_TYPE,
42+
payload: new Error('message'),
43+
error: true
44+
};
45+
}
46+
47+
/* ----- Action ----- */
48+
49+
// The Action type should cover all types above.
50+
51+
{
52+
const action: Action = {
53+
type: TEST_TYPE
54+
};
55+
56+
{
57+
// would fail if test is 'type' instread of string
58+
const test: string = action.type;
59+
}
60+
61+
{
62+
// would fail if test is in ErroneousAction type
63+
const test: StandardAction = action;
64+
}
65+
}
66+
67+
{
68+
const action: Action<TestType> = {
69+
type: TEST_TYPE
70+
};
71+
{
72+
// now action.type can be in the type of 'type'
73+
const test: TestType = action.type;
74+
}
75+
76+
{
77+
// would fail if test is in ErroneousAction type
78+
const test: StandardAction<TestType> = action;
79+
}
80+
}
81+
82+
{
83+
const action: Action<TestType> = {
84+
type: TEST_TYPE
85+
};
86+
{
87+
const test: TestType = action.type;
88+
}
89+
90+
{
91+
// should pass with any the type for Payload and Meta
92+
const test: StandardAction<TestType, any, any> = action;
93+
}
94+
}
95+
96+
{
97+
const action: Action<TestType, TestPayload, TestMeta> = {
98+
type: TEST_TYPE,
99+
payload: {
100+
data: 'payload'
101+
},
102+
meta: {
103+
data: 'meta'
104+
}
105+
};
106+
{
107+
const test: TestType = action.type;
108+
}
109+
110+
{
111+
// should pass with any the type for Payload and Meta
112+
const test: StandardAction<TestType, any, any> = action;
113+
}
114+
}
115+
116+
{
117+
const action: Action<TestType, TestPayload, TestMeta> = {
118+
type: TEST_TYPE,
119+
payload: {
120+
data: 'payload'
121+
},
122+
meta: {
123+
data: 'meta'
124+
}
125+
};
126+
127+
// would fail if test is in ErroneousAction type
128+
const test: StandardAction<TestType, TestPayload, TestMeta> = action;
129+
}
130+
131+
{
132+
const action: Action = {
133+
type: TEST_TYPE,
134+
payload: new Error('message'),
135+
error: true
136+
};
137+
138+
// would fail if test is in StandardAction type
139+
const test: ErroneousAction = action;
140+
}
141+
142+
{
143+
let action: Action;
144+
145+
{
146+
const test: boolean = action.error;
147+
}
148+
149+
if (action.error === true) {
150+
// would fail if test is in StandardAction type
151+
const test: ErroneousAction = action;
152+
} else {
153+
// would fail if test is in ErroneousAction type
154+
const test: StandardAction = action;
155+
}
156+
}
157+
158+
{
159+
let action: Action<TestType, TestPayload, TestMeta>;
160+
161+
{
162+
const test: boolean = action.error;
163+
}
164+
165+
if (action.error === true) {
166+
// would fail if test is in StandardAction type
167+
const test: ErroneousAction<TestType, TestMeta> = action;
168+
} else {
169+
// would fail if test is in ErroneousAction type
170+
const test: StandardAction<TestType, TestPayload, TestMeta> = action;
171+
}
172+
}
173+
174+
// ---------------------------------------- //

0 commit comments

Comments
 (0)