File tree 6 files changed +76
-42
lines changed
6 files changed +76
-42
lines changed Original file line number Diff line number Diff line change @@ -4,20 +4,21 @@ interface ObjectConstructor {
4
4
* @param o An object.
5
5
* @param v A property name.
6
6
*/
7
- hasOwn < Key extends PropertyKey > (
8
- o : object ,
7
+ hasOwn < Obj extends object , Key extends PropertyKey > (
8
+ o : Obj ,
9
9
v : Key
10
- ) : o is string extends Key
11
- ? { }
12
- : number extends Key
13
- ? { }
14
- : symbol extends Key
15
- ? { }
16
- : Key extends PropertyKey
17
- ? {
18
- [ key in Key ] : unknown ;
19
- }
20
- : { } ;
10
+ ) : o is Obj &
11
+ ( string extends Key
12
+ ? { }
13
+ : number extends Key
14
+ ? { }
15
+ : symbol extends Key
16
+ ? { }
17
+ : Key extends PropertyKey
18
+ ? {
19
+ [ key in Key ] : unknown ;
20
+ }
21
+ : { } ) ;
21
22
}
22
23
// /**
23
24
// * Determines whether an object has a property with the specified name.
Original file line number Diff line number Diff line change @@ -122,19 +122,21 @@ interface Object {
122
122
* Determines whether an object has a property with the specified name.
123
123
* @param v A property name.
124
124
*/
125
- hasOwnProperty < Key extends PropertyKey > (
125
+ hasOwnProperty < Obj , Key extends PropertyKey > (
126
+ this : Obj ,
126
127
v : Key
127
- ) : this is string extends Key
128
- ? { }
129
- : number extends Key
130
- ? { }
131
- : symbol extends Key
132
- ? { }
133
- : Key extends PropertyKey
134
- ? {
135
- [ key in Key ] : unknown ;
136
- }
137
- : { } ;
128
+ ) : this is Obj &
129
+ ( string extends Key
130
+ ? { }
131
+ : number extends Key
132
+ ? { }
133
+ : symbol extends Key
134
+ ? { }
135
+ : Key extends PropertyKey
136
+ ? {
137
+ [ key in Key ] : unknown ;
138
+ }
139
+ : { } ) ;
138
140
139
141
/**
140
142
* Determines whether an object exists in another object's prototype chain.
Original file line number Diff line number Diff line change @@ -4,16 +4,17 @@ interface ObjectConstructor {
4
4
* @param o An object.
5
5
* @param v A property name.
6
6
*/
7
- hasOwn < Key extends PropertyKey > (
8
- o : object ,
7
+ hasOwn < Obj extends object , Key extends PropertyKey > (
8
+ o : Obj ,
9
9
v : Key
10
- ) : o is string extends Key
11
- ? { }
12
- : number extends Key
13
- ? { }
14
- : symbol extends Key
15
- ? { }
16
- : Key extends PropertyKey
17
- ? { [ key in Key ] : unknown }
18
- : { } ;
10
+ ) : o is Obj &
11
+ ( string extends Key
12
+ ? { }
13
+ : number extends Key
14
+ ? { }
15
+ : symbol extends Key
16
+ ? { }
17
+ : Key extends PropertyKey
18
+ ? { [ key in Key ] : unknown }
19
+ : { } ) ;
19
20
}
Original file line number Diff line number Diff line change @@ -26,17 +26,18 @@ interface Object {
26
26
* Determines whether an object has a property with the specified name.
27
27
* @param v A property name.
28
28
*/
29
- hasOwnProperty < Key extends PropertyKey > (
29
+ hasOwnProperty < Obj , Key extends PropertyKey > (
30
+ this : Obj ,
30
31
v : Key
31
- ) : this is string extends Key
32
+ ) : this is Obj & ( string extends Key
32
33
? { }
33
34
: number extends Key
34
35
? { }
35
36
: symbol extends Key
36
37
? { }
37
38
: Key extends PropertyKey
38
39
? { [ key in Key ] : unknown }
39
- : { } ;
40
+ : { } ) ;
40
41
}
41
42
42
43
interface ObjectConstructor {
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { expectError, expectType } from "tsd";
6
6
const obj : object = { } ;
7
7
if ( Object . hasOwn ( obj , "foo" ) ) {
8
8
expectType < unknown > ( obj . foo ) ;
9
- expectType < { foo : unknown } > ( obj ) ;
9
+ expectType < object & { foo : unknown } > ( obj ) ;
10
10
}
11
11
const obj2 = { foo : 123 } ;
12
12
if ( Object . hasOwn ( obj2 , "bar" ) ) {
@@ -31,3 +31,17 @@ import { expectError, expectType } from "tsd";
31
31
expectType < { } > ( emptyObj ) ;
32
32
}
33
33
}
34
+
35
+ // https://github.com/uhyo/better-typescript-lib/issues/13
36
+ {
37
+ const protoObj = { protoProp : 'protoProp' } ;
38
+
39
+ const obj : Record < string , string > = Object . create ( protoObj ) ;
40
+ obj . ownProp = 'ownProp' ;
41
+
42
+ for ( const key in obj ) {
43
+ if ( ! Object . hasOwn ( obj , key ) ) continue ;
44
+ expectType < Record < string , string > > ( obj ) ;
45
+ obj [ key ] ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ expectType<{ foo: number; bar: string; baz: boolean }>(
43
43
const obj : object = { } ;
44
44
if ( obj . hasOwnProperty ( "foo" ) ) {
45
45
expectType < unknown > ( obj . foo ) ;
46
- expectType < { foo : unknown } > ( obj ) ;
46
+ expectType < object & { foo : unknown } > ( obj ) ;
47
47
}
48
48
const obj2 = { foo : 123 } ;
49
49
if ( obj2 . hasOwnProperty ( "bar" ) ) {
@@ -69,6 +69,20 @@ expectType<{ foo: number; bar: string; baz: boolean }>(
69
69
}
70
70
}
71
71
72
+ // https://github.com/uhyo/better-typescript-lib/issues/13
73
+ {
74
+ const protoObj = { protoProp : 'protoProp' } ;
75
+
76
+ const obj : Record < string , string > = Object . create ( protoObj ) ;
77
+ obj . ownProp = 'ownProp' ;
78
+
79
+ for ( const key in obj ) {
80
+ if ( ! obj . hasOwnProperty ( key ) ) continue ;
81
+ expectType < Record < string , string > > ( obj ) ;
82
+ obj [ key ] ;
83
+ }
84
+ }
85
+
72
86
// ObjectConstructor
73
87
{
74
88
// Object.defineProperty
@@ -309,4 +323,5 @@ expectType<{ foo: number; bar: string; baz: boolean }>(
309
323
}
310
324
}
311
325
312
- export { } ;
326
+ export { } ;
327
+
You can’t perform that action at this time.
0 commit comments