Skip to content

Commit 1b6b806

Browse files
Fix bugs and add tests
1 parent fa6641c commit 1b6b806

File tree

5 files changed

+87
-25
lines changed

5 files changed

+87
-25
lines changed

lib/lib.es2015.core.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ interface ObjectConstructor {
102102
* @param o The object to change its prototype.
103103
* @param proto The value of the new prototype or null.
104104
*/
105-
setPrototypeOf<T, U extends object = {}>(o: T, proto: U | null): T & U;
105+
setPrototypeOf<T extends object, U extends object>(o: T, proto: U): T & U;
106+
107+
/**
108+
* Sets the prototype of a specified object o to object proto or null. Returns the object o.
109+
* @param o The object to change its prototype.
110+
* @param proto The value of the new prototype or null.
111+
*/
112+
setPrototypeOf<T extends object>(o: T, proto: null): T;
106113
}
107114

108115
interface String {
@@ -173,7 +180,7 @@ interface String {
173180
blink(): string;
174181

175182
/**
176-
* Returns a `<bold>` HTML element
183+
* Returns a `<b>` HTML element
177184
* @deprecated A legacy feature for browser compatibility
178185
*/
179186
bold(): string;

lib/lib.es5.d.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ interface ObjectConstructor {
9797
* Creates an object that has the specified prototype or that has null prototype.
9898
* @param o Object to use as a prototype. May be null.
9999
*/
100-
create<O extends object = {}>(o: O | null): O;
100+
create<O extends object>(o: O): O;
101+
102+
/**
103+
* Creates an object that has the specified prototype or that has null prototype.
104+
* @param o Object to use as a prototype. May be null.
105+
*/
106+
create(o: null): {};
101107

102108
/**
103109
* Creates an object that has the specified prototype, and that optionally contains specified properties.
@@ -147,13 +153,16 @@ interface ObjectConstructor {
147153
o: O,
148154
p: P,
149155
attributes: D & ThisType<any>
150-
): O & {
151-
[K in P]: D extends { value: infer V }
152-
? V
153-
: D extends { get: () => infer V }
154-
? V
155-
: unknown;
156-
};
156+
): O &
157+
(P extends PropertyKey // required to make P distributive
158+
? {
159+
[K in P]: D extends { value: infer V }
160+
? V
161+
: D extends { get: () => infer V }
162+
? V
163+
: unknown;
164+
}
165+
: unknown);
157166

158167
/**
159168
* Adds one or more properties to an object, and/or modifies attributes of existing properties.
@@ -451,6 +460,22 @@ interface String {
451460
valueOf(): string;
452461

453462
readonly [index: number]: string;
463+
464+
/////////////////////////////
465+
/// ECMAScript Internationalization API
466+
/////////////////////////////
467+
468+
/**
469+
* Determines whether two strings are equivalent in the current or specified locale.
470+
* @param that String to compare to target string
471+
* @param locales A locale string or array of locale strings that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used. This parameter must conform to BCP 47 standards; see the Intl.Collator object for details.
472+
* @param options An object that contains one or more properties that specify comparison options. see the Intl.Collator object for details.
473+
*/
474+
localeCompare(
475+
that: string,
476+
locales?: string | string[],
477+
options?: Intl.CollatorOptions
478+
): number;
454479
}
455480

456481
type JSONValue =

tests/src/es2015.collection.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,3 @@ import { expectType } from "tsd";
6262
{ foo: "foo" }
6363
);
6464
}
65-
66-
export {};

tests/src/es2015.core.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import { expectType } from "tsd";
2525
expectType<{ foo: number } & { bar: string }>(obj2);
2626
const obj3 = Object.assign({ foo: 123 }, { bar: "wow" }, { baz: true });
2727
expectType<{ foo: number } & { bar: string } & { baz: boolean }>(obj3);
28+
const obj4 = Object.assign(
29+
{ foo: 123 } as { foo: number } | { bar: string },
30+
{ baz: true }
31+
);
32+
expectType<({ foo: number } | { bar: string }) & { baz: boolean }>(obj4);
33+
const obj5 = Object.setPrototypeOf({ foo: 123 }, { bar: "wow" });
34+
expectType<{ foo: number } & { bar: string }>(obj5);
2835
}
29-
30-
export {};

tests/src/es5.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@ expectType<{ foo: number }>(Object({ foo: 123 }));
88
expectType<{}>(Object(123));
99
// $ExpctType unknown
1010
expectType<unknown>(Object.getPrototypeOf([]));
11+
// Object.getOwnPropertyNames
12+
expectType<never>(Object.getOwnPropertyNames(null));
1113
// Object.create
1214
expectType<{}>(Object.create(null));
13-
// $ExpectType { foo: number; bar: string }
14-
expectType<{ foo: number; bar: string }>(
15-
Object.create(null, {
16-
foo: {
17-
value: 123,
18-
},
19-
bar: {
20-
get() {
21-
return "hi";
22-
},
15+
expectType<{ foo: number }>(Object.create({ foo: 123 }));
16+
17+
const obj = {
18+
foo: {
19+
value: 123,
20+
},
21+
bar: {
22+
get() {
23+
return "hi";
2324
},
24-
})
25+
},
26+
};
27+
const obj1 = { baz: true };
28+
expectType<{ foo: number; bar: string }>(Object.create(null, obj));
29+
expectType<{ foo: number; bar: string; baz: boolean }>(
30+
Object.create(obj1, obj)
2531
);
2632

2733
// Object
@@ -105,11 +111,32 @@ expectType<{ foo: number; bar: string }>(
105111
expectError(add2.bind({ num: 100 }, 123, 456));
106112
}
107113

114+
// NewableFunction
115+
{
116+
class Foo {
117+
constructor(private a: number, private b: number) {}
118+
}
119+
expectType<new (a: number, b: number) => Foo>(Foo.bind(null));
120+
expectType<new (b: number) => Foo>(Foo.bind(null, 123));
121+
expectType<new () => Foo>(Foo.bind(null, 123, 456));
122+
expectError(Foo.bind(null, 123, 456, 789));
123+
}
124+
108125
// IArguments
109126
(function () {
110127
expectType<unknown>(arguments[0]);
111128
})();
112129

130+
// String
131+
{
132+
"foobar".replace(/foo/g, (substr, p1, p2) => {
133+
// expectType<string>(substr);
134+
// expectType<string | number>(p1);
135+
// expectType<string | number>(p2);
136+
return "";
137+
});
138+
}
139+
113140
// JSON
114141
{
115142
expectType<JSONValue>(JSON.parse("{}"));

0 commit comments

Comments
 (0)