If I perform this type intersection:
{ constructor: 'FooConstructor', value1: boolean } & { value2: number };
The resulting type is this:
{ constructor: 'FooConstructor' & Function, value1: boolean, value2: number };
Why is the type of the resulting constructor
property intersected with Function
?
This is because:
{ constructor: 'FooConstructor', value1: boolean } & { value2: number }
is a subtype of object
, because every object type is.object['constructor']
is Function
.And that's because objects in Javascript do have a property named constructor
which refers to the function used to construct them:
> let obj = {};
> obj.constructor
ƒ Object() { [native code] }
I suggest using a property name like constructor_
or constructorName
instead, since constructor
is meant to refer to an actual constructor.