Yomguithereal / baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.
MIT License
3.15k stars 115 forks source link

Accurate argument and return types on all functions (issue #512) #513

Open qpwo opened 2 years ago

qpwo commented 2 years ago

I think this is ready for review. There's some gaps but most of it is covered. I couldn't figure out a better way to get the exports right than making an index.js and index.d.ts, but there likely is a better way. There's some gaps still with monkey business and event listeners.

Also not sure about immutability – does anything happen if the function passed to apply() mutates data? Or if you mutate the result of .get() ?

qpwo commented 2 years ago

Done making changes for the time being lmk what you think

jrust commented 2 years ago

I'm available to try this in our project if it would be helpful in moving it along. I did try bringing in the sbaobab.d.ts file, but typescript (v3.8.3) complained on a number of lines. For example:

[] | [number, ...FullPathsOf<Item>] # TS2574: A rest element type must be an array type.

and

export type FullDeepIndex<T, KS extends FullKeys> = # TS2456: Type alias 'FullDeepIndex' circularly references itself.
qpwo commented 2 years ago

@jrust easiest thing is probably npm un baobab && npm i github:qpwo/baobab#more-detailed-types then import { SBaobab } from 'baobab'; const tree = new SBaobab<MyTreeType>(myData).

Option 2 is to @ts-ignore the errors in the .d.ts file since those errors are unfortunately unavoidable it seems. The resulting types from using that file I think are all good but the errors within the file itself annoying so just ignore them.

Option 3 is to try https://github.com/qpwo/dentata (npm install dentata) which I just published today actually - it's basically a limited subset of baobab meant to improve the typescript inferencability and be a bit faster potentially.