Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to the error: Type LiveObject<Layer> does not satisfy the constraint Lson #1444

Open
max-programming opened this issue Feb 2, 2024 · 2 comments
Labels
documentation Related to the public documentation 🎪 Room

Comments

@max-programming
Copy link

max-programming commented Feb 2, 2024

What is the improvement or update you wish to see?

In the example apps I noticed the use of LiveObject<Layer> type in the Storage type. Which has a type error in (probably) the recent versions

image

Is there any context that might help us understand?

The type error can be fixed by changing the type from LiveObject<Layer> to LiveObject<Layer & LsonObject>
image

I couldn't find a solution online except for #166

Does the docs page already exist? Please link to it.

Not exactly docs but an example where I noticed: https://liveblocks.io/examples/collaborative-whiteboard-advanced/nextjs-whiteboard-advanced

@max-programming max-programming added documentation Related to the public documentation enhancement New feature or request triage needed The issue needs to be reviewed by the team labels Feb 2, 2024
@max-programming max-programming changed the title Type LiveObject<Layer> does not satisfy the constraint Lson Fix to the error: Type LiveObject<Layer> does not satisfy the constraint Lson Feb 2, 2024
@nvie
Copy link
Collaborator

nvie commented Feb 2, 2024

Hi @max-programming. Thanks for reaching out. We wrote up a troubleshooting guide about this a while ago, which explains why this error happens, what it means, and what you can do about it. Hope this helps!

I cannot get this error to show up when I try out our examples myself, so I'm not sure why this error is showing up for you. If you expand the error message further, TypeScript should show you the drill-down into the underlying reason why it thinks Layer isn't a valid JSON value. Maybe that will give some more insight?

If you have further questions, please let us know!

@max-programming
Copy link
Author

max-programming commented Feb 2, 2024

@nvie I think I now know why this happens to me. There is a difference in our types

Types in the example

export type RectangleLayer = {
  type: LayerType.Rectangle;
  x: number;
  y: number;
  height: number;
  width: number;
  fill: Color;
};

export type EllipseLayer = {
  type: LayerType.Ellipse;
  x: number;
  y: number;
  height: number;
  width: number;
  fill: Color;
};

export type PathLayer = {
  type: LayerType.Path;
  x: number;
  y: number;
  // Could be computed based on points
  height: number;
  // Could be computed based on points
  width: number;
  fill: Color;
  points: number[][];
};

export type Layer = RectangleLayer | EllipseLayer | PathLayer;

My types:

interface BaseLayer<L extends LayerType> {
  type: L;
  x: number;
  y: number;
  height: number;
  width: number;
  fill: Color;
}

export type RectangleLayer = BaseLayer<LayerType.Rectangle>

export type EllipseLayer = BaseLayer<LayerType.Ellipse>

export type PathLayer = BaseLayer<LayerType.Path> & {
  points: number[][];
};

export type Layer = RectangleLayer | EllipseLayer | PathLayer;

To be honest, it should not cause an issue because all I did was refactor the type into a reusable one BaseLayer<>
But this is the exact reason I get the error

Also no typescript does not give a deep reason to why Layer isn't JSON

@adigau adigau added the public repo label Apr 15, 2024 — with Linear
@adigau adigau removed enhancement New feature or request triage needed The issue needs to be reviewed by the team public repo labels Apr 15, 2024
@adigau adigau added the 🎪 Room label Apr 24, 2024 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Related to the public documentation 🎪 Room
Projects
None yet
Development

No branches or pull requests

3 participants