Skip to content

Commit

Permalink
Allow omitting types property for non-barrel main
Browse files Browse the repository at this point in the history
Allow omitting `types` property for non-barrel main when `.d.ts` file name matches `main`.

Fixes tsdjs#84
  • Loading branch information
BendingBender committed May 30, 2021
1 parent fc871a1 commit e5bfb3a
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 7 deletions.
7 changes: 6 additions & 1 deletion source/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export interface Options {
}

const findTypingsFile = async (pkg: any, options: Options) => {
const typings = options.typingsFile || pkg.types || pkg.typings || 'index.d.ts';
const typings =
options.typingsFile ||
pkg.types ||
pkg.typings ||
(pkg.main && path.parse(pkg.main).name + '.d.ts') ||
'index.d.ts';
const typingsExist = await pathExists(path.join(options.cwd, typings));

if (!typingsExist) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType, expectError} from '../../../..';
import one from './';

expectType<string>(one('foo', 'bar'));
expectType<number>(one(1, 2));
expectError(one(1, 2));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "foo",
"main": "index.js",
"files": [
"index.d.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
};

export default one;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expectType} from '../../..';
import {expectType, expectError} from '../../../..';
import one from './foo';

expectType<string>(one('foo', 'bar'));
expectType<number>(one(1, 2));
expectError(one(1, 2));
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"name": "foo",
"main": "foo.js",
"types": "foo.d.ts",
"files": [
"foo.js",
"foo.d.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
};

export default one;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType, expectError} from '../../../..';
import one from './';

expectType<string>(one('foo', 'bar'));
expectType<number>(one(1, 2));
expectError(one(1, 2));
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo",
"files": [
"index.d.ts"
]
}
24 changes: 21 additions & 3 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,28 @@ test('support non-barrel main', async t => {
verify(t, diagnostics, []);
});

test('support non-barrel main using `types` property', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/test-non-barrel-main-via-types')});
test('allow omitting `types` property when `main` property is missing but main is a barrel (`index.js`) and .d.ts file matches main', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/without-main')});

verify(t, diagnostics, []);
verify(t, diagnostics, [
[6, 0, 'error', 'Expected an error, but found none.']
]);
});

test('allow omitting `types` property when `main` property is set to a barrel (`index.js`) and .d.ts file matches main', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/with-main-barrel')});

verify(t, diagnostics, [
[6, 0, 'error', 'Expected an error, but found none.']
]);
});

test('allow omitting `types` property when `main` property is set to non-barrel (`foo.js`) and .d.ts file matches main', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/with-main-other')});

verify(t, diagnostics, [
[6, 0, 'error', 'Expected an error, but found none.']
]);
});

test('support testing in sub-directories', async t => {
Expand Down

0 comments on commit e5bfb3a

Please sign in to comment.