Skip to content

Commit d938a0a

Browse files
committed
Merge branch 'master' into v2
2 parents 5f9a007 + ce974c6 commit d938a0a

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
An alternative TypeScript standard library with better type definitions.
44

5+
[See the v2 branch for 2.0.0 that supports TypeScript 4.5.](https://github.com/uhyo/better-typescript-lib/tree/v2)
6+
57
## The Problem
68

79
While it is well known that TypeScript is not _very_ type safe due to the existence of `any` and other pitfalls, TypeScript's built-in type definitions are also to blame for that unsafety. For example, it is handy but very unsafe that the return type of `JSON.parse` is `any`.

lib/lib.es5.d.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,6 @@ type JSONValue =
217217
}
218218
| JSONValue[];
219219

220-
type ReadonlyJSONValue =
221-
| null
222-
| string
223-
| number
224-
| boolean
225-
| {
226-
readonly [K in string]?: ReadonlyJSONValue;
227-
}
228-
| readonly ReadonlyJSONValue[];
229-
230220
interface JSON {
231221
/**
232222
* Converts a JavaScript Object Notation (JSON) string into an object.
@@ -245,12 +235,8 @@ interface JSON {
245235
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
246236
*/
247237
stringify(
248-
value: ReadonlyJSONValue,
249-
replacer?: (
250-
this: ReadonlyJSONValue,
251-
key: string,
252-
value: ReadonlyJSONValue
253-
) => any,
238+
value: unknown,
239+
replacer?: (this: unknown, key: string, value: unknown) => any,
254240
space?: string | number
255241
): string;
256242
/**
@@ -260,7 +246,7 @@ interface JSON {
260246
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
261247
*/
262248
stringify(
263-
value: ReadonlyJSONValue,
249+
value: unknown,
264250
replacer?: (number | string)[] | null,
265251
space?: string | number
266252
): string;

tests/src/es5.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ expectType<{ foo: number; bar: string; baz: boolean }>(
8686
expectType<string>(JSON.stringify(readonlyArr));
8787
const readonlyObj = { foo: { bar: 1 } } as const;
8888
expectType<string>(JSON.stringify(readonlyObj));
89+
90+
// https://github.com/uhyo/better-typescript-lib/issues/5
91+
interface Param {
92+
id: string;
93+
value: number;
94+
}
95+
+function foo(param: Param, readonlyParam: Readonly<Param>) {
96+
JSON.stringify(param); // error
97+
JSON.stringify(readonlyParam);
98+
};
8999
}
90100

91101
// ArrayConstructor

0 commit comments

Comments
 (0)