Skip to content

Commit

Permalink
Add TypeScript definition (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 24, 2019
1 parent d443393 commit 8a3fcef
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
67 changes: 67 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as typeFest from 'type-fest';
import normalize = require('normalize-package-data');

declare namespace readPkg {
interface Options {
/**
[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data.
@default true
*/
normalize?: boolean;

/**
Current working directory.
@default process.cwd()
*/
cwd?: string;
}

interface NormalizeOptions extends Options {
normalize: true;
}

type NormalizedPackageJson = PackageJson & normalize.Package;
type PackageJson = typeFest.PackageJson;
}

declare const readPkg: {
/**
@returns The parsed JSON.
@example
```
import readPkg = require('read-pkg');
(async () => {
console.log(await readPkg());
//=> {name: 'read-pkg', …}
console.log(await readPkg({cwd: 'some-other-directory'});
//=> {name: 'unicorn', …}
})();
```
*/
(options?: readPkg.NormalizeOptions): Promise<readPkg.NormalizedPackageJson>;
(options: readPkg.Options): Promise<readPkg.PackageJson>;

/**
@returns The parsed JSON.
@example
```
import readPkg = require('read-pkg');
console.log(readPkg.sync());
//=> {name: 'read-pkg', …}
console.log(readPkg.sync({cwd: 'some-other-directory'});
//=> {name: 'unicorn', …}
```
*/
sync(options?: readPkg.NormalizeOptions): readPkg.NormalizedPackageJson;
sync(options: readPkg.Options): readPkg.PackageJson;
};

export = readPkg;
20 changes: 20 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {expectType, expectError} from 'tsd';
import readPkg = require('.');

const options: readPkg.Options = {};
expectError<readPkg.NormalizedPackageJson>({});
expectType<readPkg.PackageJson>({});

expectType<Promise<readPkg.NormalizedPackageJson>>(readPkg());
expectType<Promise<readPkg.NormalizedPackageJson>>(readPkg({normalize: true}));
expectType<Promise<readPkg.PackageJson>>(readPkg({normalize: false}));
expectError<Promise<readPkg.NormalizedPackageJson>>(
readPkg({normalize: false})
);
expectType<Promise<readPkg.PackageJson>>(readPkg({cwd: './'}));

expectType<readPkg.NormalizedPackageJson>(readPkg.sync());
expectType<readPkg.NormalizedPackageJson>(readPkg.sync({normalize: true}));
expectType<readPkg.PackageJson>(readPkg.sync({normalize: false}));
expectError<readPkg.NormalizedPackageJson>(readPkg.sync({normalize: false}));
expectType<readPkg.PackageJson>(readPkg.sync({cwd: './'}));
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"json",
Expand All @@ -31,11 +32,14 @@
"normalize"
],
"dependencies": {
"normalize-package-data": "^2.3.2",
"parse-json": "^4.0.0"
"@types/normalize-package-data": "^2.4.0",
"normalize-package-data": "^2.5.0",
"parse-json": "^4.0.0",
"type-fest": "^0.4.1"
},
"devDependencies": {
"ava": "^1.3.1",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"xo": {
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ const readPkg = require('read-pkg');
### readPkg([options])
Returns a `Promise` for the parsed JSON.
Returns a `Promise<object>` with the parsed JSON.
### readPkg.sync([options])
Returns the parsed JSON.
#### options
Type: `Object`
Type: `object`
##### cwd
Expand Down

0 comments on commit 8a3fcef

Please sign in to comment.