Giter VIP home page Giter VIP logo

Comments (4)

nvie avatar nvie commented on September 22, 2024

Hi @oravecz, we documented this issue here, in case you're interested in the details.

Ultimately, the key issue that causes this type error is on line 12:

export interface AttributeValue {
    v?: object;  // 👈 An `object` isn't guaranteed to be JSON-serializable
}

In addition, from looking at your example, your best shot will be to use type instead of interfaces, if you are in control of those type definitions, and your application allows it.

  • Change this object to JsonObject
  • Change interface to type (which aren't "open" by default, and thus helps TypeScript understand that there won't be any non-JSON fields added to the type elsewhere)

Here is an adjusted example that works around this problem.

Hope this helps to explain and solves your issue. Feel free to re-open this issue if you have any further questions! 🙏

from liveblocks.

oravecz avatar oravecz commented on September 22, 2024

Yes, that is the documentation I had referenced in the ticket.

The docs might be helped by coming out and stating "An object isn't guaranteed to be JSON-serializable"

Unfortunately for me, this interface is external to my codebase, and seems like it will prevent me from using LiveBlocks.

I'm curious also, why an "override" can't be provided for the developer to vouch that the Storage instance is serializable. I understand the desire to be pedantic. You probably fielded situations where non-serializable payloads were passed in the past, and are trying to avoid the issue.

But in doing so, you have created a hurdle for developers who need to use the TS any type. For us, this comes about because we are using OpenAPI to generate the payloads and skeletons for a Java backend, so some codegen restriction on a different language is impeding our ability to generate a more precise TS interface.

Can you say more about regarding the idea to provide developers with an option to remove the serialization check you are trying to enforce via types?

from liveblocks.

nvie avatar nvie commented on September 22, 2024

Yeah, this is tricky. One reason the types are like this is because we also use them to automatically derive the immutable JSON types from these definitions. Not enforing JSON-serializableness here will lead to many unexpected "unknowns" down the road, which can be even more confusing.

To unblock you, if you don't control the generated interface types, another workaround for you can be the following:

const attributes: (Attribute & Lson)[] = [];

new LiveList<Attribute & Lson>(attributes)
//           ^^^^^^^^^^^^^^^^ Manually tell TypeScript your Attribute type will be JSON-serializable

Playground link

Hope that helps!

from liveblocks.

oravecz avatar oravecz commented on September 22, 2024

Can we extend that Playground example a little bit more?

const attributes: (Attribute & Lson)[] = [];
const attribute = attributes[0] as unknown as (Attribute & LsonObject);

const list = new LiveList<Attribute & Lson>(attributes)
const item = new LiveObject<Attribute & LsonObject>(attribute);
list.push(item);

The last line doesn't seem to work with the types specified.

Playground Link

from liveblocks.

Related Issues (20)

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.