Skip to content

Commit 205a276

Browse files
committed
Make JSON.parse return a JSONValue instead of any
This makes type checking of things hangling JSON responses much more accurate and useful.
1 parent b534fb4 commit 205a276

4 files changed

+9
-4
lines changed

lib/lib.es5.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,14 +1039,19 @@ interface URIErrorConstructor {
10391039

10401040
declare const URIError: URIErrorConstructor;
10411041

1042+
type JSONPrimitive = string | number | boolean | null;
1043+
type JSONValue = JSONPrimitive | JSONObject | JSONArray;
1044+
type JSONObject = { [member: string]: JSONValue };
1045+
interface JSONArray extends Array<JSONValue> {}
1046+
10421047
interface JSON {
10431048
/**
10441049
* Converts a JavaScript Object Notation (JSON) string into an object.
10451050
* @param text A valid JSON string.
10461051
* @param reviver A function that transforms the results. This function is called for each member of the object.
10471052
* If a member contains nested objects, the nested objects are transformed before the parent object is.
10481053
*/
1049-
parse(text: string, reviver?: (key: any, value: any) => any): any;
1054+
parse(text: string, reviver?: (key: any, value: any) => any): JSONValue;
10501055
/**
10511056
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
10521057
* @param value A JavaScript value, usually an object or array, to be converted.

scripts/configurePrerelease.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function main(): void {
2828

2929
// Acquire the version from the package.json file and modify it appropriately.
3030
const packageJsonFilePath = normalize(args[1]);
31-
const packageJsonValue: PackageJson = JSON.parse(readFileSync(packageJsonFilePath).toString());
31+
const packageJsonValue = JSON.parse(readFileSync(packageJsonFilePath).toString()) as PackageJson;
3232

3333
const { majorMinor, patch } = parsePackageJsonVersion(packageJsonValue.version);
3434
const prereleasePatch = getPrereleasePatch(tag, patch);

scripts/generateLocalizedDiagnosticMessages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function main(): void {
136136
writeFile(
137137
path.join(outputPath, "enu", "diagnosticMessages.generated.json.lcg"),
138138
getLCGFileXML(
139-
objectToList(JSON.parse(data.toString()))
139+
objectToList(JSON.parse(data.toString()) as Record<string, string>)
140140
.sort((a, b) => a.key > b.key ? 1 : -1) // lcg sorted by property keys
141141
.reduce((s, { key, value }) => s + getItemXML(key, value), "")
142142
));

scripts/processDiagnosticMessages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function main(): void {
2727
console.log(`Reading diagnostics from ${inputFilePath}`);
2828
const inputStr = fs.readFileSync(inputFilePath, { encoding: "utf-8" });
2929

30-
const diagnosticMessagesJson: { [key: string]: DiagnosticDetails } = JSON.parse(inputStr);
30+
const diagnosticMessagesJson = JSON.parse(inputStr) as { [key: string]: DiagnosticDetails };
3131

3232
const diagnosticMessages: InputDiagnosticMessageTable = new Map();
3333
for (const key in diagnosticMessagesJson) {

0 commit comments

Comments
 (0)