-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: provide a static type checking against example usages
- Loading branch information
Showing
4 changed files
with
640 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
* *** MIT LICENSE *** | ||
* ------------------------------------------------------------------------- | ||
* This code may be modified and distributed under the MIT license. | ||
* See the LICENSE file for details. | ||
* ------------------------------------------------------------------------- | ||
* | ||
* @summary A test for the typescript definition | ||
* | ||
* @author Alvis HT Tang <alvis@hilbert.space> | ||
* @license MIT | ||
* @copyright Copyright (c) 2018 - All Rights Reserved. | ||
* ------------------------------------------------------------------------- | ||
*/ | ||
|
||
import { Action, ErroneousAction, StandardAction } from '../definitions'; | ||
|
||
// | ||
// Action Type | ||
// | ||
|
||
const TEST_TYPE = 'type'; | ||
type TestType = typeof TEST_TYPE; | ||
|
||
interface TestPayload { | ||
data: 'payload'; | ||
} | ||
|
||
interface TestMeta { | ||
data: 'meta'; | ||
} | ||
|
||
{ | ||
const action: StandardAction = { | ||
type: TEST_TYPE | ||
}; | ||
} | ||
|
||
{ | ||
const action: ErroneousAction = { | ||
type: TEST_TYPE, | ||
payload: new Error('message'), | ||
error: true | ||
}; | ||
} | ||
|
||
/* ----- Action ----- */ | ||
|
||
// The Action type should cover all types above. | ||
|
||
{ | ||
const action: Action = { | ||
type: TEST_TYPE | ||
}; | ||
|
||
{ | ||
// would fail if test is 'type' instread of string | ||
const test: string = action.type; | ||
} | ||
|
||
{ | ||
// would fail if test is in ErroneousAction type | ||
const test: StandardAction = action; | ||
} | ||
} | ||
|
||
{ | ||
const action: Action<TestType> = { | ||
type: TEST_TYPE | ||
}; | ||
{ | ||
// now action.type can be in the type of 'type' | ||
const test: TestType = action.type; | ||
} | ||
|
||
{ | ||
// would fail if test is in ErroneousAction type | ||
const test: StandardAction<TestType> = action; | ||
} | ||
} | ||
|
||
{ | ||
const action: Action<TestType> = { | ||
type: TEST_TYPE | ||
}; | ||
{ | ||
const test: TestType = action.type; | ||
} | ||
|
||
{ | ||
// should pass with any the type for Payload and Meta | ||
const test: StandardAction<TestType, any, any> = action; | ||
} | ||
} | ||
|
||
{ | ||
const action: Action<TestType, TestPayload, TestMeta> = { | ||
type: TEST_TYPE, | ||
payload: { | ||
data: 'payload' | ||
}, | ||
meta: { | ||
data: 'meta' | ||
} | ||
}; | ||
{ | ||
const test: TestType = action.type; | ||
} | ||
|
||
{ | ||
// should pass with any the type for Payload and Meta | ||
const test: StandardAction<TestType, any, any> = action; | ||
} | ||
} | ||
|
||
{ | ||
const action: Action<TestType, TestPayload, TestMeta> = { | ||
type: TEST_TYPE, | ||
payload: { | ||
data: 'payload' | ||
}, | ||
meta: { | ||
data: 'meta' | ||
} | ||
}; | ||
|
||
// would fail if test is in ErroneousAction type | ||
const test: StandardAction<TestType, TestPayload, TestMeta> = action; | ||
} | ||
|
||
{ | ||
const action: Action = { | ||
type: TEST_TYPE, | ||
payload: new Error('message'), | ||
error: true | ||
}; | ||
|
||
// would fail if test is in StandardAction type | ||
const test: ErroneousAction = action; | ||
} | ||
|
||
{ | ||
let action: Action; | ||
|
||
{ | ||
const test: boolean = action.error; | ||
} | ||
|
||
if (action.error === true) { | ||
// would fail if test is in StandardAction type | ||
const test: ErroneousAction = action; | ||
} else { | ||
// would fail if test is in ErroneousAction type | ||
const test: StandardAction = action; | ||
} | ||
} | ||
|
||
{ | ||
let action: Action<TestType, TestPayload, TestMeta>; | ||
|
||
{ | ||
const test: boolean = action.error; | ||
} | ||
|
||
if (action.error === true) { | ||
// would fail if test is in StandardAction type | ||
const test: ErroneousAction<TestType, TestMeta> = action; | ||
} else { | ||
// would fail if test is in ErroneousAction type | ||
const test: StandardAction<TestType, TestPayload, TestMeta> = action; | ||
} | ||
} | ||
|
||
// ---------------------------------------- // |
Oops, something went wrong.