I need to create type which extracts just mutable object type from existing immutable object type like:
import * as Immutable from 'seamless-immutable'
interface IObjType {
field: string;
}
type TObjImmType = Immutable.Immutable<IObjType>;
const obj: IObjType = { field: 'val' };
const objImm: TObjImmType = Immutable(obj);
// dummy function to show what I need to do
const getMutable = (immObj: TObjImmType): IObjType => immObj.asMutable();
const result = getMutable(objImm);
So the problem is with getMutable. Typescript do not checks did it reeturn mutable or immutable object and I need to force TS to validate this and throw error if immutable is returned.
How to do this?
A property being readonly is only a flag for the TS compiler, it does not exist in JavaScript.
This essentially means that readonly and non-readonly properties are 100% equal functionally, as such there should be no possible means of validating the process of making an object mutable/immutable.