Skip to content

Inferred type of getSnapshot #310

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

Closed
djres88 opened this issue Aug 17, 2017 · 3 comments
Closed

Inferred type of getSnapshot #310

djres88 opened this issue Aug 17, 2017 · 3 comments

Comments

@djres88
Copy link

djres88 commented Aug 17, 2017

Working with the latest mst (0.10.0) and liking it a lot, but we're experiencing some trouble with inferring the return type of getSnapshot, specifically when the model is instantiated in another scope. Say we have a model:

export const MyModel = types
    .model({ testing: types.string})

If we create an instance and get a snapshot in the same scope, it's able to infer that getSnapshot returns an object that includes the property "testing":

const myInstance = MyModel.create({ testing: "1,2,3" });
const mySnapshot = getSnapshot(myInstance).testing //ok --testing is a known property

However, if we're creating/assigning the instance at a later time (for example, in tests):

describe("test", function() {
  let myInstance: typeof MyModel.Type
  before(function() {
    myInstance = MyModel.create({ testing: "1,2,3" });
  })

  it("ts compile error", function() {
    const mySnapshot = getSnapshot(myInstance).testing //not ok -- inferred type of mySnapshot is {}
  })
});

There's an easy solution, ie by explicitly telling it the return type (getSnapshot<typeof MyModel.Type>...), but I'd assumed it would be able to infer the type in this case.

@djres88 djres88 changed the title Inferred type of Inferred type of getSnapshot Aug 17, 2017
@djres88
Copy link
Author

djres88 commented Aug 17, 2017

Found a different type to use that works -- ISnapshottable<typeof MyModel.Type>; Still would have assumed that .Type would include that

@mattiamanzati
Copy link
Contributor

Each model has two properties: Type and SnapshotType; due to TS lack of mapped conditional type we can't do much and always infer correctly, waiting for #117 :)

@djres88
Copy link
Author

djres88 commented Aug 17, 2017

Sounds great, will keep an eye out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants