Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow omitting types property for non-barrel main #112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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