simplytyped
Advanced tools
Comparing version 0.1.2 to 0.1.3
{ | ||
"name": "simplytyped", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "yet another Typescript type library for advanced types", | ||
@@ -5,0 +5,0 @@ "main": "src/index.ts", |
@@ -96,2 +96,11 @@ # SimplyTyped | ||
### IsType | ||
Given a base type and a value, check to see if value matches the base type. | ||
Useful for checking if something is an instance of a class. | ||
```ts | ||
class Thing { x: string }; | ||
type x = IsType<Thing, { x: string }> // => True | ||
type y = IsType<Thing, { y: number }> // => False | ||
``` | ||
## Objects | ||
@@ -98,0 +107,0 @@ |
@@ -28,2 +28,3 @@ // Conditionals | ||
export type IsType<T, X> = ReallyTrue<HasKey<T, Keys<X>>>; | ||
export type IsAny<T> = IsNever<HasKey<T, string>>; | ||
@@ -30,0 +31,0 @@ export type IsArray<T> = ReallyTrue<HasKey<T, ArrayPrototypeKeys>>; |
import test from 'ava'; | ||
import { IsAny, IsArray, IsBoolean, IsFunction, IsNumber, IsObject, IsString, True, False } from '../src/index'; | ||
import { IsAny, IsArray, IsBoolean, IsFunction, IsNumber, IsObject, IsString, IsType, True, False } from '../src/index'; | ||
function assert<T, U extends T>(t: { pass: any }) { t.pass() } | ||
function assert<T, U extends T>(t: { pass: any }) { t.pass(); } | ||
class Class { public x = 'hey' }; | ||
class Class { x = 'hey'; } | ||
const inst = new Class(); | ||
@@ -71,3 +71,3 @@ | ||
assert<IsFunction<func>, True>(t); | ||
assert<IsFunction<Function>, True>(t); | ||
assert<IsFunction<Function>, True>(t); // tslint:disable-line | ||
@@ -83,3 +83,3 @@ // TODO: this should probably be False | ||
assert<IsFunction<cls>, False>(t); | ||
}) | ||
}); | ||
@@ -102,2 +102,9 @@ test('Can check if a type is an object', t => { | ||
test('Can check if type is the same as another type', t => { | ||
assert<IsType<Class, cls>, True>(t); | ||
assert<IsType<string, str>, True>(t); | ||
assert<IsType<Class, str>, False>(t); | ||
}); | ||
test('Can check if a type is any', t => { | ||
@@ -104,0 +111,0 @@ assert<IsAny<any>, True>(t); |
33801
615
335