typescripttypesintersectionobject-type

Why, in the result of this object type intersection, is the type of the `constructor` property intersected with `Function`?


If I perform this type intersection:

{ constructor: 'FooConstructor', value1: boolean } & { value2: number };

The resulting type is this:

{ constructor: 'FooConstructor' & Function, value1: boolean, value2: number };

(Playground example)

Why is the type of the resulting constructor property intersected with Function?


Solution

  • This is because:

    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.