nimahkh / soft_bun

A lightweight and framework-agnostic reactivity system implemented in TypeScript
Other
10 stars 1 forks source link

Nested objects type safe #24

Closed nimahkh closed 1 year ago

nimahkh commented 1 year ago

In the following example, 'user.age' will throw an error

const initialState = {
        user: {
            name: "John Doe",
            age: 30,
            address: {
                street: "123 Main St",
                city: "Anytown",
                country: "USA"
            }
        },
        products: [
            { id: 1, name: "Product A", price: 10 },
            { id: 2, name: "Product B", price: 20 }
        ],
        total: 0
    };

    // Create a StateManager instance
    const largeStateManager = new StateManager(initialState);

    // Add reactive dependencies
    largeStateManager.reactive('user.age', '$user.age * 2');

Error: 'Argument of type '"user.age"' is not assignable to parameter of type '"user" | "products" | "total"'.'

Mostafa-Mohammadi commented 1 year ago

I was unable to reproduce the error after executing this code snippet in the playground. Everything appears to be functioning correctly, including the top-level total.

However, the underlying issue does not pertain to data typing. When I run this code, the age remains at 30, whereas it should have changed to 60. As I mentioned the top-level total property works fine.

Do you encounter a similar problem, or have I overlooked something in the configurations?

nimahkh commented 1 year ago

Well done Mostafa, I think it is related to another issue you already created: #25 Let's fix it there

nimahkh commented 1 year ago

Related issue https://github.com/nimahkh/soft_bun/issues/25