简体   繁体   中英

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?

Try next example:

Object.setPrototypeOf(obj, prototype)

Please see the docs

UPDATE I think it worth looking on this answer also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM