-
-
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
feature(api): tell consumers when their objects are observable #484
Conversation
Changes Unknown when pulling 081d723 on spiffytech:master into * on mobxjs:master*. |
Thanks for the PR, will consider this for the next release |
This typing is actually not correct, an object being converted to an observable is not an mobx/src/types/observableobject.ts Line 51 in aa8ea15
What could be done is introducing an empty |
Having Just to be clear, I'm not looking for TypeScript's type guards - they're designed to enhance type safety of code branches used in runtime type checks. I'm looking to improve compile-time checks, like so: @action
function updateMyFoo(foo: MyInterface & IObservableObject) {
foo.some_prop = true; // triggers observers
}
const foo: MyInterface = {some_prop: false};
updateMyFoo(foo); // compiler error - not observable
const observable_foo = observable(foo); // MyInterfyace & IObservableObject
updateMyFoo(observable_foo); // success Would you like me to update my pull request to back out the use of I could add the type guard to |
Yep, that makes sense to me :) |
@spiffytech unfirtunatelly, due to structural typing system limitation (which TYpeScript is based on), you will not receive compile time errors here, because TS will not check interfaces on object, it will check structure of object. It means, that having empty interface is almost useless. |
dôh indeed |
There is a cool trick, I've found in comments: microsoft/TypeScript#202 (comment) |
@Strate Can you clarify how your suggestion works with empty interfaces? The below code sample compiles for me, when it should produce an error - interface IObservableObject {}
interface MyInterface {
some_prop: boolean;
}
function updateMyFoo(foo: MyInterface & IObservableObject) {
foo.some_prop = true;
}
const foo: MyInterface = {some_prop: false};
const observable_foo = <MyInterface & IObservableObject> foo;
updateMyFoo(foo); // compiles :(
updateMyFoo(observable_foo); // compiles |
@mweststrate May I assume from your previous comment that you wouldn't be comfortable exposing the |
@spiffytech indeed, the something like |
Ah, I see! Confirmed, that makes the appropriate error occur in my code sample. I'll update my full request with this. |
@spiffytech did you by any chance proceed on this one ? |
8f3b8d1
to
6128be9
Compare
Yeah, I adopted what we discussed, added the type guard to I was hoping to find a way to unit test the failure case for the type guard, but everything I could think of prevented the TypeScript tests from compiling. |
Just merged, thanks a lot! |
See issue #483.