YousefED / SyncedStore

SyncedStore CRDT is an easy-to-use library for building live, collaborative applications that sync automatically.
https://syncedstore.org
MIT License
1.71k stars 51 forks source link

Example from documentation doesn't typecheck #73

Closed gleachkr closed 1 year ago

gleachkr commented 1 year ago

The minimal example from the documentation:

import { syncedStore, getYjsValue } from "@syncedstore/core";
import { WebrtcProvider } from "y-webrtc";

// (optional, define types for TypeScript)
type Vehicle = { color: string; brand: string };

// Create your SyncedStore store
export const store = syncedStore({ vehicles: [] as Vehicle[] });

// Get the Yjs document and sync automatically using y-webrtc
const doc = getYjsValue(store);
const webrtcProvider = new WebrtcProvider("my-document-id", doc);

Doesn't typecheck on line 12, because Argument of type 'Doc | AbstractType<any> | undefined' is not assignable to parameter of type 'Doc'. Type 'undefined' is not assignable to type 'Doc'.

YousefED commented 1 year ago

Thanks. Did a quick fix in https://github.com/YousefED/SyncedStore/commit/ab2155f2322d9a7a8045ccba25ec6de3b65b576d (you can simply cast with as any), but will add a getYjsDoc value later that works only on Docs and returns the correct type :)

gleachkr commented 1 year ago

Awesome. Thanks for this project!

cbenz commented 1 year ago

For that problem I have created the Y.Doc before, like this:

import * as Y from "yjs";
import { syncedStore, getYjsValue } from "@syncedstore/core";
import { WebrtcProvider } from "y-webrtc";

// (optional, define types for TypeScript)
type Vehicle = { color: string; brand: string };

const ydoc = new Y.Doc();

// Create your SyncedStore store
export const store = syncedStore({ vehicles: [] as Vehicle[] }, ydoc);

// Get the Yjs document and sync automatically using y-webrtc
const webrtcProvider = new WebrtcProvider("my-document-id", ydoc);