Socket
Socket
Sign inDemoInstall

expect-type

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expect-type - npm Package Compare versions

Comparing version 0.4.0 to 0.4.1-master-2020-03-06-00-05-12.11

dist/__tests__/boolean.test.d.ts

0

CHANGELOG.md

@@ -0,0 +0,0 @@ # Change Log

export {};
//# sourceMappingURL=index.test.d.ts.map

69

dist/__tests__/index.test.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
it('tests types', () => {
test('Type-check object references', () => {
__1.expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 });
__1.expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 });
__1.expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 });
});
test('Assertions can be inverted', () => {
__1.expectTypeOf({ a: 1 }).not.toMatchTypeOf({ b: 1 });
__1.expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 });
});
test('Catch any/unknown/never types', () => {
__1.expectTypeOf().toBeUnknown();
__1.expectTypeOf().toBeAny();
__1.expectTypeOf().toBeNever();
});
test('Test for basic javascript types', () => {
__1.expectTypeOf(() => 1).toBeFunction();

@@ -20,2 +26,4 @@ __1.expectTypeOf({}).toBeObject();

__1.expectTypeOf(Symbol(1)).toBeSymbol();
});
test('Nullable types', () => {
__1.expectTypeOf(undefined).toBeUndefined();

@@ -30,2 +38,4 @@ __1.expectTypeOf(undefined).toBeNullable();

__1.expectTypeOf().toBeNullable();
});
test('Assertions can be inverted with `.not`', () => {
__1.expectTypeOf(1).not.toBeUnknown();

@@ -37,2 +47,4 @@ __1.expectTypeOf(1).not.toBeAny();

__1.expectTypeOf(1).not.toBeNullable();
});
test('Make assertions about object properties', () => {
const obj = { a: 1, b: '' };

@@ -45,5 +57,6 @@ __1.expectTypeOf(obj)

.toEqualTypeOf();
});
test('Assert on function parameters (using `.parameter(n)` or `.parameters`) and return values (using `.return`)', () => {
const f = (a) => [a, a];
__1.expectTypeOf(f).toBeFunction();
__1.expectTypeOf('hi').not.toBeFunction();
__1.expectTypeOf(f).toBeCallableWith(1);

@@ -65,4 +78,12 @@ __1.expectTypeOf(f).not.toBeAny();

__1.expectTypeOf(twoArgFunc).parameters.toEqualTypeOf();
});
test('Promise resolution types can be checked with `.resolves`', () => {
const asyncFunc = async () => 123;
__1.expectTypeOf(asyncFunc).returns.resolves.toBeNumber();
});
test('Array items can be checked with `.items`', () => {
__1.expectTypeOf([1, 2, 3]).items.toBeNumber();
__1.expectTypeOf([1, 2, 3]).items.not.toBeString();
});
test('Check that functions never return', () => {
const thrower = () => {

@@ -72,4 +93,4 @@ throw Error();

__1.expectTypeOf(thrower).returns.toBeNever();
__1.expectTypeOf([1, 2, 3]).items.toBeNumber();
__1.expectTypeOf([1, 2, 3]).items.not.toBeString();
});
test('Generics can be used rather than references', () => {
__1.expectTypeOf().not.toEqualTypeOf();

@@ -79,40 +100,2 @@ __1.expectTypeOf().not.toEqualTypeOf();

});
it('can do boolean type logic', () => {
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
__1.expectTypeOf().toEqualTypeOf();
});
//# sourceMappingURL=index.test.js.map

@@ -42,4 +42,16 @@ export declare type Not<T extends boolean> = T extends true ? false : true;

}
/**
* Similar to Jest's `expect`, but with type-awareness.
* Gives you access to a number of type-matchers that let you make assertions about the
* form of a reference or generic type parameter.
*
* @example
* expectTypeOf({a: 1}).toMatchTypeOf({a: 2})
* expectTypeOf({a: 1}).property('a').toBeNumber()
*
* @description
* See the [full docs](https://npmjs.com/package/expect-type#documentation) for lots more examples.
*/
export declare const expectTypeOf: <Actual>(actual?: Actual | undefined) => ExpectTypeOf<Actual, true>;
export {};
//# sourceMappingURL=index.d.ts.map

@@ -5,2 +5,14 @@ "use strict";

const fn = () => true;
/**
* Similar to Jest's `expect`, but with type-awareness.
* Gives you access to a number of type-matchers that let you make assertions about the
* form of a reference or generic type parameter.
*
* @example
* expectTypeOf({a: 1}).toMatchTypeOf({a: 2})
* expectTypeOf({a: 1}).property('a').toBeNumber()
*
* @description
* See the [full docs](https://npmjs.com/package/expect-type#documentation) for lots more examples.
*/
exports.expectTypeOf = (actual) => {

@@ -7,0 +19,0 @@ const nonFunctionProperties = ['parameters', 'returns', 'resolves', 'not', 'items'];

{
"name": "expect-type",
"version": "0.4.0",
"version": "0.4.1-master-2020-03-06-00-05-12.11+b7ba300",
"repository": "https://github.com/mmkal/ts",

@@ -16,3 +16,3 @@ "homepage": "https://github.com/mmkal/ts/tree/master/packages/expect-type#readme",

},
"gitHead": "bd5431fcfc6f9a06037edeccd8a3055bc73f966b"
"gitHead": "b7ba300cebbe6cf58dd989c404b9a6cfd757e748"
}

@@ -5,4 +5,28 @@ # expect-type

## Usage
<!-- codegen:start {preset: jsdoc, module: src/index.ts, export: expectTypeOf} -->
#### [expectTypeOf](./src/index.ts#L67)
Similar to Jest's `expect`, but with type-awareness. Gives you access to a number of type-matchers that let you make assertions about the form of a reference or generic type parameter.
##### Example
```typescript
expectTypeOf({a: 1}).toMatchTypeOf({a: 2})
expectTypeOf({a: 1}).property('a').toBeNumber()
```
See the [full docs](https://npmjs.com/package/expect-type#documentation) for lots more examples.
<!-- codegen:end -->
## Contents
<!-- codegen:start {preset: md-toc, minDepth: 2, maxDepth: 5} -->
- [expectTypeOf](#expecttypeof)
- [Contents](#contents)
- [Installation and usage](#installation-and-usage)
- [Documentation](#documentation)
- [Similar projects](#similar-projects)
<!-- codegen:end -->
## Installation and usage
```cli

@@ -12,96 +36,143 @@ npm install expect-type

<!-- codegen:start {preset: regex, source: src/__tests__/index.test.ts, between: [it(', it('], header: "import {expectTypeOf} from 'expect-type'"} -->
```typescript
import {expectTypeOf} from 'expect-type'
```
it('tests types', () => {
expectTypeOf({a: 1}).toEqualTypeOf({a: 1})
expectTypeOf({a: 1, b: 1}).toMatchTypeOf({a: 1})
expectTypeOf({a: 1}).not.toMatchTypeOf({b: 1})
## Documentation
expectTypeOf({a: 1}).toEqualTypeOf({a: 2})
<!-- codegen:start {preset: jest2md, source: src/__tests__/index.test.ts} -->
Type-check object references:
expectTypeOf<unknown>().toBeUnknown()
expectTypeOf<any>().toBeAny()
expectTypeOf<never>().toBeNever()
```typescript
expectTypeOf({a: 1}).toEqualTypeOf({a: 1})
expectTypeOf({a: 1, b: 1}).toMatchTypeOf({a: 1})
expectTypeOf({a: 1}).toEqualTypeOf({a: 2})
```
expectTypeOf(() => 1).toBeFunction()
expectTypeOf({}).toBeObject()
expectTypeOf([]).toBeArray()
expectTypeOf('').toBeString()
expectTypeOf(1).toBeNumber()
expectTypeOf(true).toBeBoolean()
expectTypeOf(Promise.resolve(123)).resolves.toBeNumber()
expectTypeOf(Symbol(1)).toBeSymbol()
Assertions can be inverted:
expectTypeOf(undefined).toBeUndefined()
expectTypeOf(undefined).toBeNullable()
expectTypeOf(undefined).not.toBeNull()
```typescript
expectTypeOf({a: 1}).not.toMatchTypeOf({b: 1})
```
expectTypeOf(null).toBeNull()
expectTypeOf(null).toBeNullable()
expectTypeOf(null).not.toBeUndefined()
Catch any/unknown/never types:
expectTypeOf<1 | undefined>().toBeNullable()
expectTypeOf<1 | null>().toBeNullable()
expectTypeOf<1 | undefined | null>().toBeNullable()
```typescript
expectTypeOf<unknown>().toBeUnknown()
expectTypeOf<any>().toBeAny()
expectTypeOf<never>().toBeNever()
```
expectTypeOf(1).not.toBeUnknown()
expectTypeOf(1).not.toBeAny()
expectTypeOf(1).not.toBeNever()
expectTypeOf(1).not.toBeNull()
expectTypeOf(1).not.toBeUndefined()
expectTypeOf(1).not.toBeNullable()
Test for basic javascript types:
const obj = {a: 1, b: ''}
```typescript
expectTypeOf(() => 1).toBeFunction()
expectTypeOf({}).toBeObject()
expectTypeOf([]).toBeArray()
expectTypeOf('').toBeString()
expectTypeOf(1).toBeNumber()
expectTypeOf(true).toBeBoolean()
expectTypeOf(Promise.resolve(123)).resolves.toBeNumber()
expectTypeOf(Symbol(1)).toBeSymbol()
```
expectTypeOf(obj)
.property('a')
.toEqualTypeOf(1)
expectTypeOf(obj)
.property('b')
.toEqualTypeOf<string>()
Nullable types:
const f = (a: number) => [a, a]
```typescript
expectTypeOf(undefined).toBeUndefined()
expectTypeOf(undefined).toBeNullable()
expectTypeOf(undefined).not.toBeNull()
expectTypeOf(f).toBeFunction()
expectTypeOf('hi').not.toBeFunction()
expectTypeOf(null).toBeNull()
expectTypeOf(null).toBeNullable()
expectTypeOf(null).not.toBeUndefined()
expectTypeOf(f).toBeCallableWith(1)
expectTypeOf(f).not.toBeAny()
expectTypeOf(f).returns.not.toBeAny()
expectTypeOf(f).returns.toEqualTypeOf([1, 2])
expectTypeOf(f).returns.toEqualTypeOf([1, 2, 3])
expectTypeOf(f)
.parameter(0)
.not.toEqualTypeOf('1')
expectTypeOf(f)
.parameter(0)
.toEqualTypeOf(1)
expectTypeOf(1)
.parameter(0)
.toBeNever()
expectTypeOf<1 | undefined>().toBeNullable()
expectTypeOf<1 | null>().toBeNullable()
expectTypeOf<1 | undefined | null>().toBeNullable()
```
const twoArgFunc = (a: number, b: string) => ({a, b})
Assertions can be inverted with `.not`:
expectTypeOf(twoArgFunc).parameters.toEqualTypeOf<[number, string]>()
```typescript
expectTypeOf(1).not.toBeUnknown()
expectTypeOf(1).not.toBeAny()
expectTypeOf(1).not.toBeNever()
expectTypeOf(1).not.toBeNull()
expectTypeOf(1).not.toBeUndefined()
expectTypeOf(1).not.toBeNullable()
```
const asyncFunc = async () => 123
Make assertions about object properties:
expectTypeOf(asyncFunc).returns.resolves.toBeNumber()
```typescript
const obj = {a: 1, b: ''}
const thrower = () => {
throw Error()
}
expectTypeOf(obj)
.property('a')
.toEqualTypeOf(1)
expectTypeOf(obj)
.property('b')
.toEqualTypeOf<string>()
```
expectTypeOf(thrower).returns.toBeNever()
Assert on function parameters (using `.parameter(n)` or `.parameters`) and return values (using `.return`):
expectTypeOf([1, 2, 3]).items.toBeNumber()
expectTypeOf([1, 2, 3]).items.not.toBeString()
```typescript
const f = (a: number) => [a, a]
expectTypeOf<{a: number; b?: number}>().not.toEqualTypeOf<{a: number}>()
expectTypeOf<{a: number; b?: number | null}>().not.toEqualTypeOf<{a: number; b?: number}>()
expectTypeOf<{a: number; b?: number | null}>().toEqualTypeOf<{a: number; b?: number | null}>()
})
expectTypeOf(f).toBeFunction()
expectTypeOf(f).toBeCallableWith(1)
expectTypeOf(f).not.toBeAny()
expectTypeOf(f).returns.not.toBeAny()
expectTypeOf(f).returns.toEqualTypeOf([1, 2])
expectTypeOf(f).returns.toEqualTypeOf([1, 2, 3])
expectTypeOf(f)
.parameter(0)
.not.toEqualTypeOf('1')
expectTypeOf(f)
.parameter(0)
.toEqualTypeOf(1)
expectTypeOf(1)
.parameter(0)
.toBeNever()
const twoArgFunc = (a: number, b: string) => ({a, b})
expectTypeOf(twoArgFunc).parameters.toEqualTypeOf<[number, string]>()
```
Promise resolution types can be checked with `.resolves`:
```typescript
const asyncFunc = async () => 123
expectTypeOf(asyncFunc).returns.resolves.toBeNumber()
```
Array items can be checked with `.items`:
```typescript
expectTypeOf([1, 2, 3]).items.toBeNumber()
expectTypeOf([1, 2, 3]).items.not.toBeString()
```
Check that functions never return:
```typescript
const thrower = () => {
throw Error()
}
expectTypeOf(thrower).returns.toBeNever()
```
Generics can be used rather than references:
```typescript
expectTypeOf<{a: number; b?: number}>().not.toEqualTypeOf<{a: number}>()
expectTypeOf<{a: number; b?: number | null}>().not.toEqualTypeOf<{a: number; b?: number}>()
expectTypeOf<{a: number; b?: number | null}>().toEqualTypeOf<{a: number; b?: number | null}>()
```
<!-- codegen:end -->

@@ -108,0 +179,0 @@

@@ -1,15 +0,20 @@

import * as a from '..'
import {expectTypeOf} from '..'
it('tests types', () => {
test('Type-check object references', () => {
expectTypeOf({a: 1}).toEqualTypeOf({a: 1})
expectTypeOf({a: 1, b: 1}).toMatchTypeOf({a: 1})
expectTypeOf({a: 1}).toEqualTypeOf({a: 2})
})
test('Assertions can be inverted', () => {
expectTypeOf({a: 1}).not.toMatchTypeOf({b: 1})
})
expectTypeOf({a: 1}).toEqualTypeOf({a: 2})
test('Catch any/unknown/never types', () => {
expectTypeOf<unknown>().toBeUnknown()
expectTypeOf<any>().toBeAny()
expectTypeOf<never>().toBeNever()
})
test('Test for basic javascript types', () => {
expectTypeOf(() => 1).toBeFunction()

@@ -23,3 +28,5 @@ expectTypeOf({}).toBeObject()

expectTypeOf(Symbol(1)).toBeSymbol()
})
test('Nullable types', () => {
expectTypeOf(undefined).toBeUndefined()

@@ -36,3 +43,5 @@ expectTypeOf(undefined).toBeNullable()

expectTypeOf<1 | undefined | null>().toBeNullable()
})
test('Assertions can be inverted with `.not`', () => {
expectTypeOf(1).not.toBeUnknown()

@@ -44,3 +53,5 @@ expectTypeOf(1).not.toBeAny()

expectTypeOf(1).not.toBeNullable()
})
test('Make assertions about object properties', () => {
const obj = {a: 1, b: ''}

@@ -54,7 +65,8 @@

.toEqualTypeOf<string>()
})
test('Assert on function parameters (using `.parameter(n)` or `.parameters`) and return values (using `.return`)', () => {
const f = (a: number) => [a, a]
expectTypeOf(f).toBeFunction()
expectTypeOf('hi').not.toBeFunction()

@@ -79,7 +91,16 @@ expectTypeOf(f).toBeCallableWith(1)

expectTypeOf(twoArgFunc).parameters.toEqualTypeOf<[number, string]>()
})
test('Promise resolution types can be checked with `.resolves`', () => {
const asyncFunc = async () => 123
expectTypeOf(asyncFunc).returns.resolves.toBeNumber()
})
test('Array items can be checked with `.items`', () => {
expectTypeOf([1, 2, 3]).items.toBeNumber()
expectTypeOf([1, 2, 3]).items.not.toBeString()
})
test('Check that functions never return', () => {
const thrower = () => {

@@ -90,6 +111,5 @@ throw Error()

expectTypeOf(thrower).returns.toBeNever()
})
expectTypeOf([1, 2, 3]).items.toBeNumber()
expectTypeOf([1, 2, 3]).items.not.toBeString()
test('Generics can be used rather than references', () => {
expectTypeOf<{a: number; b?: number}>().not.toEqualTypeOf<{a: number}>()

@@ -99,48 +119,1 @@ expectTypeOf<{a: number; b?: number | null}>().not.toEqualTypeOf<{a: number; b?: number}>()

})
it('can do boolean type logic', () => {
expectTypeOf<a.And<[true, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.And<[true, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.And<[true, false]>>().toEqualTypeOf<false>()
expectTypeOf<a.And<[false, true]>>().toEqualTypeOf<false>()
expectTypeOf<a.And<[false, false]>>().toEqualTypeOf<false>()
expectTypeOf<a.Or<[true, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.Or<[true, false]>>().toEqualTypeOf<true>()
expectTypeOf<a.Or<[false, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.Or<[false, false]>>().toEqualTypeOf<false>()
expectTypeOf<a.Xor<[true, true]>>().toEqualTypeOf<false>()
expectTypeOf<a.Xor<[true, false]>>().toEqualTypeOf<true>()
expectTypeOf<a.Xor<[false, true]>>().toEqualTypeOf<true>()
expectTypeOf<a.Xor<[false, false]>>().toEqualTypeOf<false>()
expectTypeOf<a.Not<true>>().toEqualTypeOf<false>()
expectTypeOf<a.Not<false>>().toEqualTypeOf<true>()
expectTypeOf<a.IsAny<any>>().toEqualTypeOf<true>()
expectTypeOf<a.IsUnknown<any>>().toEqualTypeOf<false>()
expectTypeOf<a.IsNever<any>>().toEqualTypeOf<false>()
expectTypeOf<a.IsAny<unknown>>().toEqualTypeOf<false>()
expectTypeOf<a.IsUnknown<unknown>>().toEqualTypeOf<true>()
expectTypeOf<a.IsNever<unknown>>().toEqualTypeOf<false>()
expectTypeOf<a.IsAny<never>>().toEqualTypeOf<false>()
expectTypeOf<a.IsUnknown<never>>().toEqualTypeOf<false>()
expectTypeOf<a.IsNever<never>>().toEqualTypeOf<true>()
expectTypeOf<a.Extends<1, number>>().toEqualTypeOf<true>()
expectTypeOf<a.Extends<number, 1>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<1, 1>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<1, number>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<{a: 1}, {a: 1}>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<[{a: 1}], [{a: 1}]>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<never, never>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<any, any>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<unknown, unknown>>().toEqualTypeOf<true>()
expectTypeOf<a.Equal<any, never>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<any, unknown>>().toEqualTypeOf<false>()
expectTypeOf<a.Equal<never, unknown>>().toEqualTypeOf<false>()
})

@@ -54,2 +54,15 @@ export type Not<T extends boolean> = T extends true ? false : true

const fn: any = () => true
/**
* Similar to Jest's `expect`, but with type-awareness.
* Gives you access to a number of type-matchers that let you make assertions about the
* form of a reference or generic type parameter.
*
* @example
* expectTypeOf({a: 1}).toMatchTypeOf({a: 2})
* expectTypeOf({a: 1}).property('a').toBeNumber()
*
* @description
* See the [full docs](https://npmjs.com/package/expect-type#documentation) for lots more examples.
*/
export const expectTypeOf = <Actual>(actual?: Actual): ExpectTypeOf<Actual, true> => {

@@ -56,0 +69,0 @@ const nonFunctionProperties = ['parameters', 'returns', 'resolves', 'not', 'items'] as const

@@ -0,0 +0,0 @@ {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc