Search code examples
node.jstypescriptextendsstrict

Extending an instance (prototype) in TypeScript with strict mode enabled


I have an instance remote from a factory in a vendor script and I need to extend that instance with my own methods and members.

I found the following useful answer Extending this in Typescript class by Object.assign suggesting

BaseRemote.prototype = remote;

It seemed a clean solution at first but, I suspect due to strict mode, I'm getting the following error:

TypeError: Cannot assign to read only property 'prototype' of function 'class BaseRemote { }'

How can I extend an instance / change its prototype like that without disabling strict mode?


Solution

  • Try next example :

    Object.setPrototypeOf(obj, prototype)
    

    Please see the docs

    UPDATE I think it worth looking on this answer also