Open ajoslin opened 8 years ago
@ajoslin
observ-struct : <K, I, V <: Observ<I>>(Record<K, V>) => Observ<Record<K, I>> & Record<K, V>
I want to implement Records; which allow you to operate over a set of keys and values.
TypeScript has a feature request for this https://github.com/Microsoft/TypeScript/issues/1295
Honestly; if I wanted to a do a well typed version of observ
or observ-struct
I would start changing the interface.
aka instead of:
var state = ObservStruct({
fruits: ObservStruct({
apples: Observ(3),
oranges: Observ(5)
}),
customers: Observ(5)
})
// plain object
var state = {
fruits: {
apples: 3,
oranges: 5
},
customers: 5
}
// ObservCursor : <T>(T) => Cursor<T>
var cursor = ObservCursor(state);
// get : <T>(this: Cursor<T>) => T
cursor.get();
// update : <T>(this: Cursor<T>, path: String, value: any) => void
cursor.update('fruits.apples', 4);
var state = {
fruits: null,
customers: 5
}
var rootCursor = ObservCursor(state);
var fruitsState = {
apples: 3,
oranges: 5
}
var fruitCursor = ObservCursor(fruitsState);
// merge : <T, S>(this: Cursor<T>, keyPath: String, Cursor<S>) => void
cursor.merge('fruits', fruitCursor);
I have no idea of this ObservCursor would work. I would hope that merge()
allows us to get some of the cool features we wanted from arbitrarly nested observ...
Typescript is too inflexible to let me write a type for both the wrapped and unwrapped version of an observ-struct without writing two separate type definitions.