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

Add TypeScript typings #29

Closed
wants to merge 7 commits into from
Closed
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
43 changes: 43 additions & 0 deletions index.d.ts
@@ -0,0 +1,43 @@
export interface IOptions {
/**
* Directory to start from.
* @default process.cwd()
*/
cwd: string;
}

/**
* Returns a `Promise` for either the filepath or `null` if it couldn't be found.
*
* @param filename - Filename of the file to find.
*/
declare function findUp(
filename: string,
options?: IOptions,
): Promise<string | null>;
/**
* Returns a `Promise` for either the first filepath found (by respecting the order) or `null` if none could be found.
*
* @param filename - Filename of the file to find.
*/
declare function findUp(
filename: string[],
options?: IOptions,
): Promise<string | null>;

declare namespace findUp {
/**
* Returns a filepath or null.
*
* @param filename - Filename of the file to find.
*/
function sync(filename: string, options?: IOptions): string | null;
/**
* Returns the first filepath found (by respecting the order) or null.
*
* @param filename - Filename of the file to find.
*/
function sync(filename: string[], options?: IOptions): string | null;
}

export default findUp;
42 changes: 42 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,42 @@
import { expectType } from 'tsd-check';
import findUp from '.';

(async () => {
let path = await findUp('package.json');
if (typeof path === 'string') {
expectType<string>(path);
} else {
expectType<null>(path);
}
path = await findUp('package.json', { cwd: './' });
if (typeof path === 'string') {
expectType<string>(path);
} else {
expectType<null>(path);
}
path = await findUp(['package.json']);
if (typeof path === 'string') {
expectType<string>(path);
} else {
expectType<null>(path);
}

path = findUp.sync('package.json');
if (typeof path === 'string') {
expectType<string>(path);
} else {
expectType<null>(path);
}
path = findUp.sync('package.json', { cwd: './' });
if (typeof path === 'string') {
expectType<string>(path);
} else {
expectType<null>(path);
}
path = findUp.sync(['package.json']);
if (typeof path === 'string') {
expectType<string>(path);
} else {
expectType<null>(path);
}
})();
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"find",
Expand Down Expand Up @@ -45,6 +46,7 @@
"devDependencies": {
"ava": "*",
"tempy": "^0.2.1",
"tsd-check": "^0.2.1",
"xo": "*"
}
}