pmndrs/valtio

Typescrpt error when try to have a computed value derived from another computed

Nunatic02 opened this issue · 2 comments

const state = proxyWithComputed<{ count: number }, { doubled: number, quadrupled: number }>({
  count: 1,
}, {
  doubled: snap => snap.count * 2,
  quadrupled: snap => snap.doubled * 2
})

will result in typescript error Property 'doubled' does not exist on type '{ count: number; }'.

Thanks for reporting. This is a limitation in types. (I think it's impossible, but would like to hear from TS experts.)

A workaround would only be type assertion.

const state = proxyWithComputed<{ count: number }, { doubled: number, quadrupled: number }>({
  count: 1,
}, {
  doubled: snap => snap.count * 2,
  quadrupled: snap => (snap as { count: number; doubled: number }).doubled * 2
})

https://codesandbox.io/s/trusting-bas-doyoq?file=/src/App.tsx

Closing this as it's not possible in TS.