Skip to content

Commit

Permalink
Allow omitting types property for non-barrel main (#112)
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 #84
  • Loading branch information
BendingBender committed Jun 1, 2021
1 parent 3b61da7 commit f1a2057
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
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
@@ -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));
@@ -0,0 +1,7 @@
{
"name": "foo",
"main": "index.js",
"files": [
"index.d.ts"
]
}
@@ -0,0 +1,6 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
};

export default one;
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
@@ -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));
@@ -1,9 +1,7 @@
{
"name": "foo",
"main": "foo.js",
"types": "foo.d.ts",
"files": [
"foo.js",
"foo.d.ts"
]
}
@@ -0,0 +1,6 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
};

export default one;
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
@@ -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));
@@ -0,0 +1,6 @@
{
"name": "foo",
"files": [
"index.d.ts"
]
}
24 changes: 21 additions & 3 deletions source/test/test.ts
Expand Up @@ -146,10 +146,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 f1a2057

Please sign in to comment.