File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -89,6 +89,16 @@ class HasPropertyClassExample {
8989const hasPropertyClassExample = new HasPropertyClassExample ( ) ;
9090hasProperty ( hasPropertyClassExample , 'a' ) ;
9191
92+ type HasPropertyTypeExample = {
93+ a ?: number ;
94+ } ;
95+
96+ // It keeps the original type when defined.
97+ const hasPropertyTypeExample : HasPropertyTypeExample = { } ;
98+ if ( hasProperty ( hasPropertyTypeExample , 'a' ) ) {
99+ expectType < number | undefined > ( hasPropertyTypeExample . a ) ;
100+ }
101+
92102//=============================================================================
93103// RuntimeObject
94104//=============================================================================
Original file line number Diff line number Diff line change @@ -93,8 +93,11 @@ export const hasProperty = <
9393> (
9494 objectToCheck : ObjectToCheck ,
9595 name : Property ,
96- ) : objectToCheck is ObjectToCheck & Record < Property , unknown > =>
97- Object . hasOwnProperty . call ( objectToCheck , name ) ;
96+ ) : objectToCheck is ObjectToCheck &
97+ Record <
98+ Property ,
99+ Property extends keyof ObjectToCheck ? ObjectToCheck [ Property ] : unknown
100+ > => Object . hasOwnProperty . call ( objectToCheck , name ) ;
98101
99102export type PlainObject = Record < number | string | symbol , unknown > ;
100103
You can’t perform that action at this time.
0 commit comments