mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.9k stars 639 forks source link

Add float (or finite) type #1960

Closed davetapley closed 11 months ago

davetapley commented 1 year ago

Discussed in https://github.com/mobxjs/mobx-state-tree/discussions/1959

Originally posted by **davetapley** September 15, 2022 Since we already have an `integer` type: https://github.com/mobxjs/mobx-state-tree/blob/6a50d79a114811b492b98c37b0bbb35d6c9d9155/packages/mobx-state-tree/src/types/primitives.ts#L113-L117 Do we want one or both of these? 1. A `float` type, which would restrict against `NaN`. 2. A `finite` type, which would restrict against `NaN` and infinities (taking inspiration from [`isFinite`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite)). I'll concede that both could be implemented with `refinement`, but so could `integer` and we already have that. This would have saved me from a bug where I am using `parseFloat` and a `NaN` was sneaking in to my model. It would be great if I could just specify `float` in my model and have MST throw, instead of having to check for `NaN` manually. --- Happy to make a PR if it will be accepted 🤞🏻
coolsoftwaretyler commented 1 year ago

Hey @davetapley - I know it's been a while. I can't answer whether or not a PR would be accepted, but I agree this makes sense to do. I can ask around the maintainers and try to get a sense of what they're thinking in terms of adding functionality like this. Are you still interested in writing a PR for it? If not, no worries, I know it's been quite a while since you brought this up. I just wanted you to at least get an acknowledgment here, albeit belated.

coolsoftwaretyler commented 1 year ago

For folks who aren't following the discussion, we are open for PRs to add new types! Here's the thread: https://github.com/mobxjs/mobx-state-tree/discussions/1959

I'll leave this open for Dave to attempt a PR if they're still interested. But if not, I'll take this on myself and close out the issue once it's all set.