Skip to content

Commit a484e54

Browse files
committed
feat(ts-definitions): Improve TypeScript definitions, add tests for types
1 parent 49e230d commit a484e54

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

index.d.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
declare module 'fast-json-stable-stringify' {
2-
function stringify(obj: any): string;
3-
export = stringify;
1+
export = stringify;
2+
3+
declare function stringify(
4+
obj: any,
5+
options?: stringify.Options | stringify.Comparator
6+
): string;
7+
8+
declare namespace stringify {
9+
interface Options {
10+
cmp?: Comparator;
11+
cycles?: boolean;
12+
}
13+
14+
type Comparator = (a: CompareDescriptor, b: CompareDescriptor) => number;
15+
16+
interface CompareDescriptor {
17+
key: string;
18+
value: any;
19+
}
420
}

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
"json-stable-stringify": "latest",
1515
"nyc": "^14.1.0",
1616
"pre-commit": "^1.2.2",
17-
"tape": "^4.11.0"
17+
"tape": "^4.11.0",
18+
"tsd": "^0.15.1"
1819
},
1920
"scripts": {
2021
"eslint": "eslint index.js test",
2122
"test-spec": "tape test/*.js",
22-
"test": "npm run eslint && nyc npm run test-spec"
23+
"test-types": "tsd",
24+
"test": "npm run eslint && nyc npm run test-spec && npm run test-types"
2325
},
2426
"repository": {
2527
"type": "git",
@@ -49,6 +51,15 @@
4951
"text-summary"
5052
]
5153
},
54+
"tsd": {
55+
"directory": "test",
56+
"compilerOptions": {
57+
"lib": [
58+
"es2015"
59+
],
60+
"skipLibCheck": true
61+
}
62+
},
5263
"engines": {
5364
"node": ">=8"
5465
}

test/index.test-d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expectType } from "tsd";
2+
import stringify = require("../index");
3+
4+
// test type exports
5+
type CompareDescriptor = stringify.CompareDescriptor;
6+
type Comparator = stringify.Comparator;
7+
type Options = stringify.Options;
8+
9+
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
10+
11+
expectType<string>(stringify(obj));
12+
expectType<string>(
13+
stringify(obj, (a, b) => {
14+
expectType<CompareDescriptor>(a);
15+
expectType<CompareDescriptor>(b);
16+
return a.key < b.key ? 1 : -1;
17+
})
18+
);
19+
expectType<string>(
20+
stringify(obj, {
21+
cmp(a, b) {
22+
expectType<CompareDescriptor>(a);
23+
expectType<CompareDescriptor>(b);
24+
return a.key < b.key ? 1 : -1;
25+
},
26+
})
27+
);
28+
expectType<string>(stringify(obj, { cycles: true }));

0 commit comments

Comments
 (0)