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

TypeScript type for nested, optional reactive objects is broken #68

Open
irskep opened this issue Apr 8, 2023 · 1 comment
Open

TypeScript type for nested, optional reactive objects is broken #68

irskep opened this issue Apr 8, 2023 · 1 comment

Comments

@irskep
Copy link

irskep commented Apr 8, 2023

Let's say I make this reactive object:

interface State {
  x?: { y: number };
}

const state = reactive<State>({x: { y: 0 }});

And then I want to observe the value of x.y:

state.x?.$on('y', (val) => console.log(val));

I get a TypeScript error:

Property '$on' does not exist on type '{ y: number; }'

I recognize this example code won't work if the initial value of x is undefined, but if it's present, .$on works fine, so it would be nice if the type system reflected this.

@justin-schroeder
Copy link
Owner

justin-schroeder commented Apr 10, 2023

Yeah this is unfortunately true. Sadly there is no way (at the moment) to define different types on setting vs getting with proxy objects (see: microsoft/TypeScript#43826). I'll leave this open to encourage us to look into it further as other options arise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants