You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In #22 we added objectHasOwn. It's a type guard with return type to be object is (ObjectType & Record<Key, unknown>)
microsoft/TypeScript#21732 suggested a similar type guard inOperator which has exactly the same return type of objectHasOwn. However, inOperator can assert property existence in prototypes while objectHasOwn can not.
I propose a new function objectHas here, which should have the same implementation as inOperator. The naming changed to objectHas to align with objectHasOwn.
Proposing implementation
exportfunctionobjectHas<ObjectType,KeyextendsPropertyKey>(object: ObjectType,key: Key,): object is (ObjectType&Record<Key,unknown>){returnkeyinobject;}
What's the difference between objectHasOwn and objectHas?
#47 proposing a new function keyIn that has the exact same function body but a different type definition and return type guard. They result in different type predicates. See microsoft/TypeScript#43284 (comment)
When to use keyIn, native in operator and objectHas
in operator
Native in operator is good at narrowing in union types. For example, #30 can be resolved by just using the in operator.
consta: PromiseSettledResult<string>={reason: '1',status: "rejected"}if('reason'ina){// a is now PromiseRejectedResult}
Maybe it's just me, but, I don't see much utility in any of these functions other than objectHasOwn. I find that it can be very easy to make mistakes in the left-hand side of the in operator, especially when a property is renamed. I prefer to use discriminated unions whenever possible in my own code.
I personally would not use objectHas either as I would not use foo in object. I only use objectHasOwn. Another concern with objectHas is that it's easy to type, so a user might reach for it without realizing objectHasOwn is better in most scenarios. So thinking more about this, if we were to add such a utility, it needs a more verbose name.
In #22 we added
objectHasOwn
. It's a type guard with return type to beobject is (ObjectType & Record<Key, unknown>)
microsoft/TypeScript#21732 suggested a similar type guard
inOperator
which has exactly the same return type ofobjectHasOwn
. However,inOperator
can assert property existence in prototypes whileobjectHasOwn
can not.I propose a new function
objectHas
here, which should have the same implementation asinOperator
. The naming changed toobjectHas
to align withobjectHasOwn
.Proposing implementation
What's the difference between
objectHasOwn
andobjectHas
?See https://tc39.es/ecma262/multipage/abstract-operations.html#sec-hasproperty and https://tc39.es/ecma262/multipage/abstract-operations.html#sec-hasownproperty
What's the difference between this issue and #47?
#47 proposing a new function
keyIn
that has the exact same function body but a different type definition and return type guard. They result in different type predicates. See microsoft/TypeScript#43284 (comment)When to use
keyIn
, native in operator andobjectHas
in
operatorNative
in
operator is good at narrowing in union types. For example, #30 can be resolved by just using thein
operator.Playground
(But be careful that
promiseSettledResults.filter(p => 'reason' in p)
won't work as expected right without defining your ownisRejected
type guard.)keyIn
keyIn
should be used when you want to narrow the type of key to specific literals.Playground
objectHas
objectHas
should be used when you want to safely check the existence of a property? I'm still confused about the use cases of this function.The text was updated successfully, but these errors were encountered: