Skip to content

Commit ee75309

Browse files
Improve types of Object and Object.prototype
1 parent ead95b7 commit ee75309

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

lib/lib.es2015.core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ interface ObjectConstructor {
100100
* @param o The object to change its prototype.
101101
* @param proto The value of the new prototype or null.
102102
*/
103-
setPrototypeOf<T>(o: T, proto: object | null): T;
103+
setPrototypeOf<T, U extends object = {}>(o: T, proto: U | null): T & U;
104104
}
105105

106106
interface String {

lib/lib.es5.d.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,39 @@ interface ObjectConstructor {
8989
* on that object, and are not inherited from the object's prototype. The properties of an object include both fields (objects) and functions.
9090
* @param o Object that contains the own properties.
9191
*/
92-
getOwnPropertyNames(o: any): string[];
92+
getOwnPropertyNames<O>(o: O): O extends undefined | null ? never : string[];
9393

9494
/**
9595
* Creates an object that has the specified prototype or that has null prototype.
9696
* @param o Object to use as a prototype. May be null.
9797
*/
98-
create(o: object | null): {};
98+
create<O extends object = {}>(o: O | null): O;
99+
100+
/**
101+
* Creates an object that has the specified prototype, and that optionally contains specified properties.
102+
* @param o Object to use as a prototype. May be null
103+
* @param properties JavaScript object that contains one or more property descriptors.
104+
*/
105+
create<O extends object, P extends Record<PropertyKey, PropertyDescriptor>>(
106+
o: O,
107+
properties: P & ThisType<any>
108+
): {
109+
[K in keyof (O & P)]: P[K] extends { value: infer V }
110+
? V
111+
: P[K] extends { get: () => infer V }
112+
? V
113+
: K extends keyof O
114+
? O[K]
115+
: unknown;
116+
};
99117

100118
/**
101119
* Creates an object that has the specified prototype, and that optionally contains specified properties.
102120
* @param o Object to use as a prototype. May be null
103121
* @param properties JavaScript object that contains one or more property descriptors.
104122
*/
105123
create<P extends Record<string, PropertyDescriptor>>(
106-
o: object | null,
124+
o: null,
107125
properties: P & ThisType<any>
108126
): {
109127
[K in keyof P]: P[K] extends { value: infer V }
@@ -119,28 +137,31 @@ interface ObjectConstructor {
119137
* @param p The property name.
120138
* @param attributes Descriptor for the property. It can be for a data property or an accessor property.
121139
*/
122-
defineProperty<O, K extends PropertyKey, D extends PropertyDescriptor>(
140+
defineProperty<
141+
O extends object,
142+
P extends PropertyKey,
143+
D extends PropertyDescriptor
144+
>(
123145
o: O,
124-
p: K,
146+
p: P,
125147
attributes: D & ThisType<any>
126-
): O &
127-
(K extends PropertyKey
128-
? Record<
129-
K,
130-
D extends { value: infer V }
131-
? V
132-
: D extends { get: () => infer V }
133-
? V
134-
: unknown
135-
>
136-
: unknown);
148+
): O & {
149+
[K in P]: D extends { value: infer V }
150+
? V
151+
: D extends { get: () => infer V }
152+
? V
153+
: unknown;
154+
};
137155

138156
/**
139157
* Adds one or more properties to an object, and/or modifies attributes of existing properties.
140158
* @param o Object on which to add or modify the properties. This can be a native JavaScript object or a DOM object.
141159
* @param properties JavaScript object that contains one or more descriptor objects. Each descriptor object describes a data property or an accessor property.
142160
*/
143-
defineProperties<O, P extends Record<PropertyKey, PropertyDescriptor>>(
161+
defineProperties<
162+
O extends object,
163+
P extends Record<PropertyKey, PropertyDescriptor>
164+
>(
144165
o: O,
145166
properties: P & ThisType<any>
146167
): {

0 commit comments

Comments
 (0)