Skip to content

Commit 72d656d

Browse files
committed
README
1 parent 24db201 commit 72d656d

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# better-typescript-lib
2+
3+
An alternative TypeScript standard library with better type definitions.
4+
5+
## The Problem
6+
7+
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`.
8+
9+
Ideally TypeScript's built-in type definitions should have been better in view of type safety, but it is nearly impossible if we take backward compatibility into account.
10+
11+
## The Solution
12+
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.
14+
15+
## Usage
16+
17+
### Install
18+
19+
```sh
20+
npm i -D better-typescript-lib
21+
```
22+
23+
### Remove built-in library from tsconfig.json
24+
25+
```diff
26+
- "lib": ["es5", "dom"]
27+
+ "noLib": true
28+
```
29+
30+
### Include better-typescript-lib
31+
32+
Include better-typescript-lib with triple-slash directives from the entry point of your code. Note that these directives must be placed at the very top of a `.ts` file.
33+
34+
```ts
35+
/// <reference path="./node_modules/better-typescript-lib/lib.es5.d.ts" />
36+
/// <reference path="./node_modules/better-typescript-lib/lib.dom.d.ts" />
37+
```
38+
39+
## Contributing
40+
41+
Welcome

build/build.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdir, readdir, writeFile } from "fs/promises";
1+
import { mkdir, readdir, rm, symlink, writeFile } from "fs/promises";
22
import path from "path";
33
import ts from "typescript";
44
import { replacement } from "./replacement";
@@ -8,9 +8,14 @@ const distDir = path.join(projectDir, "dist", "lib");
88
const tsDir = path.join(projectDir, "TypeScript");
99

1010
async function main() {
11+
await rm(distDir, {
12+
force: true,
13+
recursive: true,
14+
});
1115
await mkdir(distDir, {
1216
recursive: true,
1317
});
18+
await symlink(path.join(projectDir, "lib"), path.join(distDir, "better"));
1419

1520
// copy TypeScript lib files
1621
const libs = await readdir(path.join(tsDir, "lib"));
@@ -27,7 +32,7 @@ async function main() {
2732
continue;
2833
}
2934
const repl = replacement.get(libFile);
30-
let result = repl ? `/// <reference path="../../lib/${libFile}" />\n` : "";
35+
let result = repl ? `/// <reference path="./better/${libFile}" />\n` : "";
3136
if (!repl) {
3237
for (const statement of file.statements) {
3338
result += statement.getFullText(file);

build/package.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "path";
33

44
const projectDir = process.env.PROJECT || process.cwd();
55
const libDir = path.join(projectDir, "dist", "lib");
6+
const betterDir = path.join(projectDir, "lib");
67
const templateDir = path.join(projectDir, "package-template");
78
const packageDir = path.join(projectDir, "dist-package");
89

@@ -11,24 +12,34 @@ async function main() {
1112
force: true,
1213
recursive: true,
1314
});
14-
await mkdir(packageDir, {
15+
await mkdir(path.join(packageDir, "better"), {
1516
recursive: true,
1617
});
1718

1819
// copy package template
1920
// copy lib files
2021
const files = (await readdir(libDir))
22+
.filter((libFile) => /\.d\.ts$/.test(libFile))
2123
.map((libFile) => path.join(libDir, libFile))
2224
.concat(
2325
(await readdir(templateDir)).map((file) => path.join(templateDir, file))
2426
);
2527
await Promise.all(
26-
files.map(async (libFile) => {
27-
await writeFile(
28-
path.join(packageDir, path.basename(libFile)),
29-
await readFile(libFile)
30-
);
31-
})
28+
files
29+
.map(async (libFile) => {
30+
await writeFile(
31+
path.join(packageDir, path.basename(libFile)),
32+
await readFile(libFile)
33+
);
34+
})
35+
.concat(
36+
(await readdir(betterDir)).map(async (libFile) => {
37+
await writeFile(
38+
path.join(packageDir, "better", libFile),
39+
await readFile(path.join(betterDir, libFile))
40+
);
41+
})
42+
)
3243
);
3344
}
3445

0 commit comments

Comments
 (0)