Skip to content
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

getRoot typings in MST3 #951

Closed
6 tasks done
PMExtra opened this issue Jul 27, 2018 · 8 comments
Closed
6 tasks done

getRoot typings in MST3 #951

PMExtra opened this issue Jul 27, 2018 · 8 comments
Labels
has PR A Pull Request to fix the issue is available

Comments

@PMExtra
Copy link

PMExtra commented Jul 27, 2018

I have:

  • I think something is not working as it should.
    • I've checked documentation and searched for existing issues
    • I've made sure your project is based on the latest MST version
    • Fork this code sandbox or another minimal reproduction.
    • Describe expected behavior
    • Describe observed behavior

In MST2:

const RootStoreModel = types.props({ subStore: SubStore })
type RootStore = typeof RootStoreModel.Type

const SubStoreModel = types.actions(self => ({
  someFunc() {
    let rootStore = getRoot<RootStore>(self)
    // I got a variable `rootStore: RootStore`
  }
}))

In MST3:

    let rootStore = getRoot<RootStore>(self)
    // I got an error TS2344: Types 'blabla' does not satisfy the constraint 'IAnyType'
    // Then I got a variable `rootStore: ExtractIStateTreeNode<RootStore, ExtractC<RootStore>, ExtractS<RootStore>, ExtractT<RootStore>>`
    // And I cannot get auto completion or definition of RootStore in WebStorm.

Everything looks fine at runtime even the type was wrong.
What's wrong with MST3?

@xaviergonz
Copy link
Contributor

xaviergonz commented Jul 29, 2018

do you have something named 'blabla' somewhere? if so, what does it look like?

@PMExtra
Copy link
Author

PMExtra commented Jul 30, 2018

@xaviergonz It's just equals typeof RootStoreModel.Type.
qq20180730-162332 2x

@xaviergonz
Copy link
Contributor

ah thanks, I'll take a look this afternoon

@xaviergonz
Copy link
Contributor

xaviergonz commented Jul 30, 2018

@PMExtra actually try getRoot<typeof RootStoreModel>

@xaviergonz
Copy link
Contributor

I'll make later a PR so it supports both modes

@PMExtra
Copy link
Author

PMExtra commented Jul 30, 2018

@xaviergonz Thank you very much. So it's recommended to use typeof RootStoreModel instead typeof RootStoreModel.Type, right?

Now I have a template that like below:

SomeStoreModel.ts

    export const SomeStoreModel = types.blabla...
    export type SomeStore = typeof SomeStoreModel.Type
    export type SomeStoreSnapshot = typeof SomeStoreModel.SnapshotType

Should I replace all typeof SomeStoreModel.Type with typeof SomeStoreModel ?

@xaviergonz
Copy link
Contributor

After the PR I created gets merged you will basically be able to use both ways and both ways should work.
Just bear in mind that for non-model types (e.g. maps and arrays) you have to use typeof Array/Map.

This is, after the PR all this will work

    const C = types.model({ a: 123 })

    // model as root
    const ModelWithC = types.model({ c: C })
    const modelInstance = ModelWithC.create({ c: C.create() })

    // getRoot
    const modelRoot1 = getRoot<typeof ModelWithC>(modelInstance.c)
    const modelCR1: typeof C.Type = modelRoot1.c
    const modelRoot2 = getRoot<typeof ModelWithC.Type>(modelInstance.c)
    const modelCR2: typeof C.Type = modelRoot2.c

    // getParent
    const modelParent1 = getParent<typeof ModelWithC>(modelInstance.c)
    const modelCP1: typeof ModelWithC.Type = modelParent1
    const modelParent2 = getParent<typeof ModelWithC.Type>(modelInstance.c)
    const modelCP2: typeof ModelWithC.Type = modelParent2

    // array as root
    const ArrayOfC = types.array(C)
    const arrayInstance = ArrayOfC.create([C.create()])

    // getRoot
    const arrayRoot1 = getRoot<typeof ArrayOfC>(arrayInstance[0])
    const arrayCR1: typeof C.Type = arrayRoot1[0]

    // getParent
    const arrayParent1 = getParent<typeof ArrayOfC>(arrayInstance[0])
    const arrayCP1: typeof ArrayOfC.Type = arrayParent1

    // map as root
    const MapOfC = types.map(C)
    const mapInstance = MapOfC.create({ a: C.create() })

    // getRoot
    const mapRoot1 = getRoot<typeof MapOfC>(mapInstance.get("a")!)
    const mapC1: typeof C.Type = mapRoot1.get("a")!

    // getParent
    const mapParent1 = getRoot<typeof MapOfC>(mapInstance.get("a")!)
    const mapCP1: typeof MapOfC.Type = mapParent1

@xaviergonz xaviergonz added the has PR A Pull Request to fix the issue is available label Aug 2, 2018
@mweststrate
Copy link
Member

Fix released as 3.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has PR A Pull Request to fix the issue is available
Projects
None yet
Development

No branches or pull requests

3 participants