Package Exports
- conditional-type-checks
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (conditional-type-checks) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Conditional Type Checks
As TypeScript's type system becomes more complex it's useful to be able to write tests for what a type should be.
This library offers reusable conditional types to do these checks.
Type Checks
These will resolve to the type true when they match and false otherwise.
IsNullable<T>- Checks ifTis possiblynullorundefined.IsExact<T, U>- Checks ifTexactly matchesU.Has<T, U>- Checks ifThasU.NotHas<T, U>- Checks ifTdoes not haveU.IsAny<T>- Checks ifTis theanytype.IsNever<T>- Checks ifTis thenevertype.IsUnknown<T>- Checks ifTis theunknowntype.- More to come...
Ways to Test
Use what you prefer:
- The
AssertTrue,AssertFalse, orAsserttypes. - The
assertfunction.
Use with AssertTrue, AssertFalse, and Assert
Doing a test:
import { AssertTrue, AssertFalse, Has, IsNever, IsNullable } from "conditional-type-checks";
const result = someFunction(someArg);
type doTest = AssertTrue<Has<typeof result, string> | IsNullable<typeof result>>
| AssertFalse<IsNever<typeof result>>
| Assert<Has<typeof result, number>, true>;Warning: Do not use an intersection type between checks (ex. Has<string | number, string> & IsNever<never>) because it will cause everything to pass if only one of the checks passes.
Use with assert
Doing a test:
import { assert, IsExact } from "conditional-type-checks";
const result = someFunction(someArg);
// compile error if the type of `result` is not exactly `string | number`
assert<IsExact<typeof result, string | number>>(true);Failure:
// causes a compile error that `true` is not assignable to `false`
assert<IsNullable<string>>(true); // string is not nullableInstall
npm install --save-dev conditional-type-checks