xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
549 stars 25 forks source link

Accessing Ref.id #174

Closed hoshinokanade closed 4 years ago

hoshinokanade commented 4 years ago

I want to access the underlying id that the Ref is pointing to.

Ref.d.ts

declare const Ref_base: import("../model/Model")._Model<{
    /**
     * Reference id.
     */
    id: import("..").ModelProp<string, string, never>; // it is there, but didn't show up in intellisense
}>;
/**
 * A reference model base type.
 * Use `customRef` to create a custom ref constructor.
 */
export declare abstract class Ref<T extends object> extends Ref_base {
    protected abstract resolve(): T | undefined;
    /**
     * The object this reference points to, or `undefined` if the reference is currently invalid.
     */
    get maybeCurrent(): T | undefined;
    /**
     * If the reference is currently valid.
     */
    get isValid(): boolean;
    /**
     * The object this reference points to, or throws if invalid.
     */
    get current(): T;
}

Looks like that we could access Ref.id but it indeed didn't show up in intellisense. When serialized into json after getting snapshot,

{
        id: '8-b8OnHCwTKEB4wrXCnSbCicKBUQoS', // I want this!
        '$modelId': '9-b8OnHCwTKEB4wrXCnSbCicKBUQoS',
        '$modelType': 'MyModelRef'
}

Is this intentional? I am trying to explicitly fire some (easily serializable) events. In those events, I want to carry the underlying id of what Ref is trying to reference to.

typescript 3.9.5 mobx-keystone 0.45.1

hoshinokanade commented 4 years ago

on the other hand, Ref.$modelId and Ref.$modelType is also missing from intellisense. Even with getSnapshot(myRef), the type hint is SnapshotOutOfObject<Ref<MyModel>>, which I expect it should be SnapshotOutOfModel<Ref<MyModel>>. I don't know whether it is by-design or not.

xaviergonz commented 4 years ago

Which IDE are you using? In theory a model .id (as well as .$modelId and $modelType should be accessible out of the box.

Could you also paste some code?

hoshinokanade commented 4 years ago

I checked my package.json and is now able to make it to display correctly. Seems my "globally installed" typescript version was too old. If anyone is encountering the same issue, run tsc -v and ensure you get > 3.9