Giter Club home page Giter Club logo

yjs-types's Introduction

yjs-types

npm

Refined TypeScript types for Yjs

This project currently is an experiment, things may change

Yjs is an excellent library, but it has relatively weak TypeScript support. This package adds a few new types to improve developer experience with Yjs:

  • TypedMap - a heavily typed version of Yjs Map
  • TypedArray - updated version of Yjs Array
  • TypedDoc - a typed version of Yjs Doc

Related Yjs issues:

Note

These types do not guarantee the real state of the document. First of all, you can use @ts-ignore or any type to violate type guarantees. Second, Yjs document can be updated from outside.

To be sure, you really want to have some sort of runtime validation. Ideally, you would infer types from that validation system, and use them with these library types.

I could define four level of types accuracy:

  1. True - types should not lie.
    Example: undefined as a return type for the get methods of TypedMap and TypedArray.

  2. Should be true - can be broken only with ts-ignore, any or foreign actions.
    Example: TypedMap set, has and delete methods.

  3. Possible true - generally true, but can be easily broken with TS.
    Example: Types of iterator, keys, values, entries and forEach methods of TypedMap.

  4. False - types are lying.
    No examples here.

yjs-types types are implemented intentionally at level Possible true.

This practice is common in TypeScript, take a look at the example:

const getData = () => ({ a: "123", b: 42, c: null })

const data: { a: string, b: number } = getData();

for (const [k, v] of Object.entries(data)) {
    //         ^? const v: string | number - null is absent
}

Get Started

Install:

npm install --save-dev yjs-types

Then, import:

import { TypedMap } from "yjs-types";

Examples

Yjs standard Map typing can be defined with TypedMap like this:

type Bar = {
    "prop1": string;
    "prop2": Y.Text;
}

const m = new Y.Map() as TypedMap<{ [key: string]: Bar }>;

m.get("someString") // type is Bar | undefined

Object like typing with TypedMap:

type Bar = {
  "prop1": string;
  "prop2": Y.Text;
}

const m = new Y.Map() as TypedMap<Bar>;

m.get("prop2") // type is Y.Text | undefined
m.get("unknownProp") // TS reports error on "unknownProp", type is string | Y.Text | undefined

TypedDoc example:

type Bar = {
    "prop1": string;
    "prop2": Y.Text;
}

const doc = new Y.Doc() as TypedDoc<{ "map1": TypedMap<Bar> }, { "array1": TypedArray<Bar> }>;

doc.getMap("map1") // type is TypedMap<Bar>
doc.getArray("array1") // type is TypedArray<Bar>

Issues

Constructor is not covered by yjs-types

The nature of current types is such, that Yjs constructor for Map and Array can't be covered by yjs-types.

Proper type for Doc.get(...)

It's too complicated, or it may even be impossible to define a good type for it.

Merging it to Yjs

I think it would be fantastic, but it would require breaking changes, which would most likely have to be implemented in JSDoc..

Usage of any in generic arguments

I could not solve some errors in TypedDoc if I defined Y.Map or Y.Array with an unknown argument.

yjs-types's People

Contributors

strangecamelcaselogin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.