Skip to content

Commit

Permalink
test: provide a static type checking against example usages
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Jul 3, 2018
1 parent cb908ca commit 28fae65
Show file tree
Hide file tree
Showing 4 changed files with 640 additions and 0 deletions.
174 changes: 174 additions & 0 deletions test/typecheck.action.ts
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;
}
}

// ---------------------------------------- //

0 comments on commit 28fae65

Please sign in to comment.