-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove observable.box type inconsistancy #3409
Conversation
Unlike `observable.map`, `observable.set`, and `observable.array` where it is possible to create a type correct empty instance without any initial values but like `observable.object` I think that `observable.box` should require an initial value. This is because the following code type checks but will throw an error at runtime: ```ts import { observable } from "mobx"; const myObvs = observable.box<string>(); myObvs.get().includes("hello world"); // this will throw a TypeError ```
🦋 Changeset detectedLatest commit: 353fff7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Signed-off-by: Sebastian Malton <sebastian@malton.name>
@urugator For me it seems like a valid change. What are your thoughts on it? I can't think of a usecase where |
What does |
It means that TS is trying to process a file that shouldn't be processed. A possible cause is the new |
I need the new tsconfig file to make sure that |
Signed-off-by: Sebastian Malton <sebastian@malton.name>
LGTM, thanks both of you. |
CI got stuck again or whats the status here? |
Hi @Nokel81 Let me clarify. It's better to avoid adding a new |
@urugator @Nokel81 I'd be happy to hear your opinion on that: What if we just forbid to create
const a = observable.box<string | undefined>(undefined); // IObservableValue<string | undefined>
const b = observable.box<string | null>(null); // IObservableValue<string | null>
The downsides is that it is kinda BC break. But in my opinion it's a bug fix rather than a BC break. This solution also forces a developer to be more explicit, but this explicity also helps TS to infer types correctly. I've tried to remove the |
I think it's not a design issue, but a typesystem problem. If something can exist without a value, it feels somehow natural to make that value optional and on the contrary a bit artifical/weird to actually require |
@kubk what do you mean that they fail even without the new tsconfig? They pass with the new tsconfig. As far as I can see CI is green. Without the tsconfig then |
I've added a few changes to the PR to check that. First I added a line that won't compile in TS strict mode: 5491476 , then I removed the new config file: 6e06dc6 Here is related CI job: https://app.circleci.com/pipelines/github/mobxjs/mobx/2025/workflows/b569591f-17a6-4b57-82fb-6905af65506d/jobs/8916 It fails with an error: "Type 'null' is not assignable to type 'string'". It means that tests are already being executed in TS strict mode and we don't have to add a new config file. Lastly I've replaced Here we can be more specific and check exact types to avoid false positives in the future. CI has passed so I'll merge the PR. Thank you for your help and patience 👍 |
@urugator Thanks for you input! Indeed there is no best choice, so let's avoid BC break at least. |
Unlike
observable.map
,observable.set
, andobservable.array
where it is possible to create a type correct empty instance without any initial values but likeobservable.object
I think thatobservable.box
should require an initial value.This is because the following code type checks but will throw an error at runtime:
Code change checklist
/docs
. For new functionality, at leastAPI.md
should be updatedyarn mobx test:performance
)