Skip to content

Commit 00fbedf

Browse files
committed
readme
1 parent 72d656d commit 00fbedf

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Ideally TypeScript's built-in type definitions should have been better in view o
1010

1111
## The Solution
1212

13-
This package provides an alternative type definitions which are safer than TypeScript's built-in ones. With this package, TypeScript users obtain less chance of unexpectedly getting `any` values. This package also includes other improved, stricter type definitions.
13+
This package provides an alternative type definitions which are safer than TypeScript's built-in ones. With this package, TypeScript users obtain less chance of unexpectedly getting `any` values. For example, in this type definition the return type of `JSON.parse` is not `any`, but `JSONData` which represents all possible JSON data.
14+
15+
This package also includes other improved, stricter type definitions.
1416

1517
## Usage
1618

@@ -20,7 +22,7 @@ This package provides an alternative type definitions which are safer than TypeS
2022
npm i -D better-typescript-lib
2123
```
2224

23-
### Remove built-in library from tsconfig.json
25+
### Disable built-in library from tsconfig.json
2426

2527
```diff
2628
- "lib": ["es5", "dom"]
@@ -36,6 +38,22 @@ Include better-typescript-lib with triple-slash directives from the entry point
3638
/// <reference path="./node_modules/better-typescript-lib/lib.dom.d.ts" />
3739
```
3840

41+
## Supported TypeScript Versions
42+
43+
| better-typescript-lib | TypeScript |
44+
| --------------------- | --------------- |
45+
| 1.0.0 | TS 4.1 or later |
46+
47+
## Concepts
48+
49+
Better-typescript-lib is _not_ meant to be compatible with TypeScript's built-in library. Therefore it is the most suitable to new TypeScript projects. An existing project may also adopt better-typescript-lib but additional type errors need to be fixed.
50+
51+
As this is only an alternative to TypeScript's built-in type definitions, we have no plan of providing any runtime implemention through better-typescript-lib.
52+
53+
### Versioning Policy
54+
55+
Improvements to type definitions may be released as a new minor version even if it may cause new type errors to existing codebases.
56+
3957
## Contributing
4058

4159
Welcome

build/replacement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const replacement = new Map([
88
"IArguments",
99
"JSON",
1010
"ArrayConstructor",
11+
"PromiseConstructorLike",
1112
]),
1213
],
1314
[

lib/lib.es2015.promise.d.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ interface PromiseConstructor {
1414
*/
1515
new <T>(
1616
executor: (
17-
resolve: {
18-
(value: T | PromiseLike<T>): void;
19-
} & (undefined extends T
17+
resolve: undefined extends T
2018
? {
2119
(value?: T | PromiseLike<T>): void;
2220
}
23-
: {}),
21+
: {
22+
(value: T | PromiseLike<T>): void;
23+
},
24+
// TODO: Revisit after https://github.com/microsoft/TypeScript/issues/42156 solves
25+
// {
26+
// (value: T | PromiseLike<T>): void;
27+
// } & (undefined extends T
28+
// ? {
29+
// (value?: T | PromiseLike<T>): void;
30+
// }
31+
// : {}),
2432
reject: (reason?: any) => void
2533
) => void
2634
): Promise<T>;
@@ -32,7 +40,7 @@ interface PromiseConstructor {
3240
* @returns A new Promise.
3341
*/
3442
all<T extends readonly any[]>(
35-
values: T
43+
values: [...T]
3644
): Promise<
3745
{
3846
[K in keyof T]: Awaited<T[K]>;

lib/lib.es5.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,16 @@ interface ArrayConstructor {
257257
isArray(arg: any): arg is unknown[];
258258
readonly prototype: unknown[];
259259
}
260+
261+
declare type PromiseConstructorLike = new <T>(
262+
executor: (
263+
resolve: undefined extends T
264+
? {
265+
(value?: T | PromiseLike<T>): void;
266+
}
267+
: {
268+
(value: T | PromiseLike<T>): void;
269+
},
270+
reject: (reason?: any) => void
271+
) => void
272+
) => PromiseLike<T>;

tests/es2015.promise.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ new Promise((resolve) => {
2929
expectType<string>(v2);
3030
expectType<boolean>(v3);
3131

32+
const a = await Promise.all([num, str, bool]);
33+
expectType<number>(a[0]);
34+
expectType<string>(a[1]);
35+
expectType<boolean>(a[2]);
36+
3237
const ps: Promise<string | number>[] = [num, str, num];
3338
const [v4, v5, v6] = await Promise.all(ps);
3439
expectType<string | number>(v4);
@@ -41,3 +46,7 @@ new Promise((resolve) => {
4146
const p2 = Promise.resolve<Promise<number>>(p1);
4247
expectType<Promise<number>>(p2);
4348
}
49+
50+
(async function (): Promise<number> {
51+
return 3;
52+
});

tests/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "esnext",
3+
"target": "es5",
44
"module": "esnext",
55
"moduleResolution": "node",
66
"noEmit": true,

0 commit comments

Comments
 (0)