Skip to content

Commit cf6ad99

Browse files
committed
complete es5
1 parent f5501a8 commit cf6ad99

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

build/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ async function main() {
3636
}
3737
}
3838
result += file.text.slice(file.endOfFileToken.pos);
39+
// Replace <reference lib="" /> to <reference path="" />
40+
result = result.replace(
41+
/^\/\/\/\s*<reference\s+lib="(.+?)"\s*\/>/gm,
42+
(_, lib) => `/// <reference path="./lib.${lib}.d.ts" />`
43+
);
3944

4045
await writeFile(path.join(distDir, libFile), result);
4146
console.log(libFile);

build/replacement.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ export const replacement = new Set<string>([
22
// lib.es5.d.ts
33
"eval",
44
"ObjectConstructor",
5+
"CallableFunction",
6+
"IArguments",
7+
"JSON",
8+
"ArrayConstructor",
59
]);
610

711
export const betterLibs = new Set<string>(["lib.es5.d.ts"]);

lib/lib.es5.d.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,105 @@ interface ObjectConstructor {
154154
*/
155155
keys(o: object): string[];
156156
}
157+
158+
interface CallableFunction extends Function {
159+
/**
160+
* Calls the function with the specified object as the this value and the elements of specified array as the arguments.
161+
* @param thisArg The object to be used as the this object.
162+
* @param args An array of argument values to be passed to the function.
163+
*/
164+
apply<T, R>(this: (this: T) => R, thisArg: T): R;
165+
apply<T, A extends any[], R>(
166+
this: (this: T, ...args: A) => R,
167+
thisArg: T,
168+
args: A
169+
): R;
170+
171+
/**
172+
* Calls the function with the specified object as the this value and the specified rest arguments as the arguments.
173+
* @param thisArg The object to be used as the this object.
174+
* @param args Argument values to be passed to the function.
175+
*/
176+
call<T, A extends any[], R>(
177+
this: (this: T, ...args: A) => R,
178+
thisArg: T,
179+
...args: A
180+
): R;
181+
182+
/**
183+
* For a given function, creates a bound function that has the same body as the original function.
184+
* The this object of the bound function is associated with the specified object, and has the specified initial parameters.
185+
* @param thisArg The object to be used as the this object.
186+
* @param args Arguments to bind to the parameters of the function.
187+
*/
188+
bind<T, A extends readonly any[], B extends readonly any[], R>(
189+
this: (this: T, ...args: [...A, ...B]) => R,
190+
thisArg: T,
191+
...args: A
192+
): (...args: B) => R;
193+
}
194+
195+
interface IArguments {
196+
[index: number]: unknown;
197+
length: number;
198+
callee: Function;
199+
}
200+
201+
type JSONValue =
202+
| null
203+
| string
204+
| number
205+
| boolean
206+
| {
207+
[K in string]?: JSONValue;
208+
}
209+
| JSONValue[];
210+
211+
interface JSON {
212+
/**
213+
* Converts a JavaScript Object Notation (JSON) string into an object.
214+
* @param text A valid JSON string.
215+
* @param reviver A function that transforms the results. This function is called for each member of the object.
216+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
217+
*/
218+
parse(
219+
text: string,
220+
reviver?: (this: JSONValue, key: string, value: JSONValue) => any
221+
): JSONValue;
222+
/**
223+
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
224+
* @param value A JavaScript value, usually an object or array, to be converted.
225+
* @param replacer A function that transforms the results.
226+
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
227+
*/
228+
stringify(
229+
value: JSONValue,
230+
replacer?: (this: JSONValue, key: string, value: JSONValue) => any,
231+
space?: string | number
232+
): string;
233+
/**
234+
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
235+
* @param value A JavaScript value, usually an object or array, to be converted.
236+
* @param replacer An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
237+
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
238+
*/
239+
stringify(
240+
value: JSONValue,
241+
replacer?: (number | string)[] | null,
242+
space?: string | number
243+
): string;
244+
}
245+
246+
/**
247+
* An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
248+
*/
249+
declare var JSON: JSON;
250+
251+
interface ArrayConstructor {
252+
new <T>(arrayLength: number): T[];
253+
new <T>(...items: T[]): T[];
254+
<T>(arrayLength: number): T[];
255+
<T>(...items: T[]): T[];
256+
isArray(arg: any): arg is unknown[];
257+
readonly prototype: unknown[];
258+
}

tests/es5.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path="../dist/lib/lib.es5.d.ts" />
22

3-
import { expectType } from "tsd";
3+
import { expectError, expectType } from "tsd";
44

55
// eval
66
expectType<unknown>(eval("foo"));
@@ -54,4 +54,59 @@ expectType<{ foo: number; bar: string; baz: boolean }>(
5454
)
5555
);
5656

57+
// CallableFunction
58+
{
59+
const add = (a: number, b: number): number => a + b;
60+
expectType<(a: number, b: number) => number>(add.bind(null));
61+
expectType<(b: number) => number>(add.bind(null, 123));
62+
expectType<() => number>(add.bind(null, 123, 456));
63+
expectError(add.bind(null, 123, 456, 789));
64+
65+
const add2 = function (this: { num: number }, a: number): number {
66+
return this.num + a;
67+
};
68+
expectError(add2.bind(null));
69+
expectError(add2.bind(null, 123));
70+
expectType<(a: number) => number>(add2.bind({ num: 0 }));
71+
expectType<() => number>(add2.bind({ num: 100 }, 123));
72+
expectError(add2.bind({ num: 100 }, 123, 456));
73+
}
74+
75+
// IArguments
76+
(function () {
77+
expectType<unknown>(arguments[0]);
78+
})();
79+
80+
// JSON
81+
expectType<JSONValue>(JSON.parse("{}"));
82+
83+
// ArrayConstructor
84+
{
85+
const a1 = new Array();
86+
expectType<unknown[]>(a1);
87+
const a2 = new Array<number>();
88+
expectType<number[]>(a2);
89+
const a3: number[] = new Array();
90+
expectType<number[]>(a3);
91+
const a4 = new Array("foo", "bar");
92+
expectType<string[]>(a4);
93+
94+
const a5 = new Array(1);
95+
expectType<unknown[]>(a5);
96+
97+
const a6 = new Array<boolean>(1);
98+
expectType<boolean[]>(a6);
99+
100+
const a7: boolean[] = new Array(1);
101+
expectType<boolean[]>(a7);
102+
103+
const a8: {} = {};
104+
if (Array.isArray(a7)) {
105+
expectType<boolean>(a7[0]);
106+
}
107+
if (Array.isArray(a8)) {
108+
expectType<unknown>(a8[0]);
109+
}
110+
}
111+
57112
export {};

0 commit comments

Comments
 (0)