Extending Yjs Types without breaking everything

Hi there I’m currently developing https://archibase.dev with Yjs to provide real-time database schema edition.

I came with the idea to create custom Yjs types to extends defaults shared types methods and to get a better type experience DX, code clarity etc.
For example


export class YColumn extends Map<any> { 
...

  setConstraints = (key:string, value:any) => {
    const type = this.get("constraints") as YMap<any>
    type.set(key, value)
  };

...
}

// this allows me to perform conveniently a 
column.setConstraints("default", true)
// whereever I have a reference to it. 

But when we encode state as update or play with the history, custom types are loss with their functionalities and the app crashes as expected.

Is this something possible, or should I move all the methods elsewhere and leave the Shared types alone?

I tackled this via composition rather than inheritance, as I ran into similar roadblocks.

1 Like

I’m would recommend you to check out runtime type checking libraries like zod and io-ts. These check that arbitrary JSON conforms to a defined type, and this defined type interoperates perfectly with typescript as well (so you don’t need to write your type definitions twice and risk inconsistencies.)

1 Like

Thanks all, wiil do :slight_smile: