Skip to content

Commit f4b4f77

Browse files
committed
json-util: adjust to stricter types of json-stable-stringify
Version 1.2.0 of said package now comes with Typescript-style types, and that pointed out that the result of the `stringify()` function may very well be `undefined` instead of a string. Let's adapt to that. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent bc023dc commit f4b4f77

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/json-util.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ export function fromJSON<T>(input: string): T {
55
}
66

77
export function toJSON<T>(input: T): string {
8-
return stringify(input);
8+
const result = stringify(input);
9+
if (typeof result !== "string") throw new Error(`Could not convert ${input} to JSON`);
10+
return result;
911
}
1012

1113
export function toPrettyJSON<T>(input: T): string {
12-
return stringify(input, { space: 4 });
14+
const result = stringify(input, { space: 4 });
15+
if (typeof result !== "string") throw new Error(`Could not convert ${input} to JSON`);
16+
return result;
1317
}

0 commit comments

Comments
 (0)