diff --git a/readme.md b/readme.md index 431e049e..78a894c5 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/source/lib/assertions/assert.ts b/source/lib/assertions/assert.ts index 522d3dd9..055eca9e 100644 --- a/source/lib/assertions/assert.ts +++ b/source/lib/assertions/assert.ts @@ -8,6 +8,16 @@ export const expectType = (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 = (value: T) => { // tslint:disable-line:no-unused + // Do nothing, the TypeScript compiler handles this for us +}; + /** * Assert the value to throw an argument error. * diff --git a/source/test/assignability.ts b/source/test/assignability.ts new file mode 100644 index 00000000..c043be0a --- /dev/null +++ b/source/test/assignability.ts @@ -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\'.'] + ]); +}); diff --git a/source/test/fixtures/assignability/assignable/index.d.ts b/source/test/fixtures/assignability/assignable/index.d.ts new file mode 100644 index 00000000..266914ab --- /dev/null +++ b/source/test/fixtures/assignability/assignable/index.d.ts @@ -0,0 +1,6 @@ +declare const concat: { + (foo: string, bar: string): string; + (foo: number, bar: number): number; +}; + +export default concat; diff --git a/source/test/fixtures/assignability/assignable/index.js b/source/test/fixtures/assignability/assignable/index.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/assignability/assignable/index.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/assignability/assignable/index.test-d.ts b/source/test/fixtures/assignability/assignable/index.test-d.ts new file mode 100644 index 00000000..d0583acf --- /dev/null +++ b/source/test/fixtures/assignability/assignable/index.test-d.ts @@ -0,0 +1,8 @@ +import {expectAssignable} from '../../../..'; +import concat from '.'; + +expectAssignable(concat('foo', 'bar')); +expectAssignable(concat(1, 2)); +expectAssignable(concat(1, 2)); + +expectAssignable(concat('unicorn', 'rainbow')); diff --git a/source/test/fixtures/assignability/assignable/package.json b/source/test/fixtures/assignability/assignable/package.json new file mode 100644 index 00000000..8392a5e3 --- /dev/null +++ b/source/test/fixtures/assignability/assignable/package.json @@ -0,0 +1,6 @@ +{ + "name": "foo", + "dependencies": { + "rxjs": "^6.5.3" + } +}