Utility TypeScript @ 2.0.0-beta
    Preparing search index...

    Namespace Test

    Utility types for unit testing TypeScript types.

    Types derived from and inspired by MichiganTypeScript/type-testing (GitHub) and Testing Types in TypeScript by Adam Rackis.

    0.1.0

    import { Test } from '@maddimathon/utility-typescript/types';
    

    Your type tests should probably be in the same place as your Javascript tests (and not where those objects are defined). (So replace this with your imported item(s) to test.)

    declare function toTest( param: boolean ): boolean;
    declare function toTest( param: string ): string;
    declare function toTest( param: boolean | string ): boolean | string;

    These values should also be tested by Jest/etc. for their values, but this example is just looking at type testing.

    const _tests_toTest = {
    bool: toTest( true ),
    string: toTest( 'string' ),
    either: toTest( 'unk' as boolean | string ),
    };

    If any of these tests fail, the Expect type will cause an error.

    // (export does nothing but avoid errors since these files don't go to prod!)
    export type Test_toTest = [

    // testing the general return
    Test.Expect<Test.Exactly<ReturnType<typeof toTest>, boolean | string>>,

    // testing the override results
    Test.Expect<Test.Exactly<typeof _tests_toTest.bool, boolean>>,
    Test.Expect<Test.Exactly<typeof _tests_toTest.string, string>>,
    Test.Expect<Test.Exactly<typeof _tests_toTest.either, boolean | string>>,

    // testing what should fail is also useful
    Test.ExpectNot<Test.Exactly<typeof _tests_toTest.bool, string>>,
    Test.ExpectNot<Test.Exactly<typeof _tests_toTest.string, boolean>>,
    Test.ExpectNot<Test.Exactly<typeof _tests_toTest.either, any>>,
    Test.ExpectNot<Test.Exactly<typeof _tests_toTest.either, boolean>>,
    Test.ExpectNot<Test.Exactly<typeof _tests_toTest.either, string>>,
    ];

    Type Aliases

    Exactly

    Tests if two types are exactly the same shape.

    Expect

    Demands the parameter evaluate to true.

    ExpectNot

    Demands the parameter evaluate to false. Inverse of Expect.

    IsArray

    Tests if a type is a valid array.

    Satisfies

    Tests if a given type would satisfy a given supertype.

    Equivalent

    Tests if the provided arguments resolve to equivalent TypeScript values.