I think this might be a bit of an oversight in how narrowing from any
was implemented. Lodash's isObject()
method is a user-defined type guard that narrows its argument if it returns true
, but this interacts with any
in a way that you are not happy with.
Originally, when one invoked a type guard that narrows to some specific object type on a value of type any
, it would not narrow at all and stay any
. This bothered people, because the compiler should be able to catch errors like this:
interface Foo { x: string; }
declare function isFoo(x: any): x is Foo;
declare const v: any;
if (isFoo(v)) {
v // still any
v.x.toUperCase(); // no error?
}
This was raised in microsoft/TypeScript#9999 and fixed in microsoft/TypeScript#10334. So now one gets the following desirable behavior:
if (isFoo(v)) {
v // Foo
v.x.toUperCase(); // error!
// Property 'toUperCase' does not exist on type 'string'. Did you mean 'toUpperCase'?
}
But the intent was not to narrow from any
no matter what. The intended logic is represented in this comment: narrowing from any
to primitives or specific object types is desirable, but narrowing to Object
or Function
is not, because those types are nearly useless and any
is preferable.
So if lodash's isObject()
were typed like the following:
interface LoDashStatic {
isObject(value?: any): value is Object; // Object
}
then you wouldn't have the problem:
if (_.isObject(myObjectVariable)) {
// still any
this.anotherVariable = myObjectVariable.propertyName; // okay
}
But isObject()
is actually declared like this:
interface LoDashStatic {
isObject(value?: any): value is object; // lowercase o object
}
That's using object
, not Object
. The object
type means "non-primitive object" and was introduced in TypeScript 2.2 via microsoft/TypeScript#12501. That was after the narrow-from-any
PR was done.
In other words: at the time the logic for when to narrow from any
was hashed out, there was no object
type to consider. Possibly they would have included object
along with Object
and Function
as an exception. Or maybe not; it's hard to say for certain. Maybe someone could even file a new issue in GitHub asking to prevent narrowing from any
to object
using the issues linked here as justification.
But for now, that's just the way it is, and any
is narrowed to object
:
if (_.isObject(myObjectVariable)) {
// object
this.anotherVariable = myObjectVariable.propertyName; // error!
}
So what can you do as a workaround? Well, you could always use your own custom typing for isObject()
that uses Object
instead, via declaration merging:
// merge into lodash typings
declare module 'lodash' {
interface LoDashStatic {
isObject(value?: any): value is Object;
}
}
Or you could use type assertions to "widen" back to any
:
this.anotherVariable = (myObjectVariable as any).propertyName; // error!
Or, since any
is a problematic type in general, you might want to give myObjectVariable
a more accurate type, representing what you actually know about it. For example, maybe you know it could either be a string
, a number
, or some object with a propertyName
property of type string
. Then you'd use this instead:
const myObjectVariable: string | number | { propertyName: string } = service.getObject();
if (_.isObject(myObjectVariable)) {
// {propertyName: string}
this.anotherVariable = myObjectVariable.propertyName; // okay
}
and be happy that the compiler removed string
and number
and left you with the object type. (That's just an example; you could use { [k: string]: any } | undefined
instead... or some other union of types in which the object
-compatible one can be indexed at the propertyName
key.)
If you can't do this in your code then you can't, but it's what I'd recommend most strongly because it uses TypeScript's compiler to help keep your code type safe, instead of looking for ways to keep your code unsafe by persisting any
.
Anyway, hope that explains the situation and gives you some options to proceed. Good luck!
Playground link
_.isObject
returns true. That is pretty cool. I guess an easy fix would be to cast to anythis.anotherVariable = (myObjectVariable as any).propertyName;