Skip to content

Commit

Permalink
Add expectAssignable expectation - fixes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Nov 12, 2019
1 parent 206573b commit 0968824
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ These options will be overridden if a `tsconfig.json` file is found in your proj

Check that `value` is identical to type `T`.

### expectAssignable<T>(value)

Check that `value` is assignable to type `T`.

### expectError(function)

Check if the function call has argument type errors.
Expand Down
10 changes: 10 additions & 0 deletions source/lib/assertions/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export const expectType = <T>(value: T) => { // tslint:disable-line:no-unused
// Do nothing, the TypeScript compiler handles this for us
};

/**
* Check that `value` is assignable to type `T`.
*
* @param value - Value that should be assignable to type `T`.
*/
// @ts-ignore
export const expectAssignable = <T>(value: T) => { // tslint:disable-line:no-unused
// Do nothing, the TypeScript compiler handles this for us
};

/**
* Assert the value to throw an argument error.
*
Expand Down
12 changes: 12 additions & 0 deletions source/test/assignability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as path from 'path';
import test from 'ava';
import {verify} from './fixtures/utils';
import tsd from '..';

test('assignable', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/assignability/assignable')});

verify(t, diagnostics, [
[8, 26, 'error', 'Argument of type \'string\' is not assignable to parameter of type \'boolean\'.']
]);
});
6 changes: 6 additions & 0 deletions source/test/fixtures/assignability/assignable/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const concat: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
};

export default concat;
3 changes: 3 additions & 0 deletions source/test/fixtures/assignability/assignable/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
8 changes: 8 additions & 0 deletions source/test/fixtures/assignability/assignable/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {expectAssignable} from '../../../..';
import concat from '.';

expectAssignable<string | number>(concat('foo', 'bar'));
expectAssignable<string | number>(concat(1, 2));
expectAssignable<any>(concat(1, 2));

expectAssignable<boolean>(concat('unicorn', 'rainbow'));
6 changes: 6 additions & 0 deletions source/test/fixtures/assignability/assignable/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo",
"dependencies": {
"rxjs": "^6.5.3"
}
}

0 comments on commit 0968824

Please sign in to comment.