Mobx-state-tree: Using the Typescript compiler to generate model definitions from classes

Created on 19 May 2017  路  4Comments  路  Source: mobxjs/mobx-state-tree

Could a Typescript class contain all the necessary information via types and annotations to generate a model definition?

Just some transformer examples:
https://github.com/Microsoft/TypeScript/issues/14419#issuecomment-298814505

question

All 4 comments

Hello!
Ideally yes, but it would be restricted and not provide all the features MST provides. For example unions may provide a dispatch to determine which type resolve if the value is ambiguos, and that's impossible with TS. Another example is default values.

You can absolutely do the opposite, if you have a MST model, you can extract a valid TS interface by doing as described here https://github.com/mobxjs/mobx-state-tree/blob/master/README.md#typescript--mst
Feel free to close the issue if I answered all your questions! :)

Thank you for the quick answer!
The idea of having "no new api" seemed tempting. But I guess practically and maintenance wise the possible gain would be negligible.

@mattiamanzati There's no problem with default values at all, is there?

class Foo {
    bar: string = "default value";
}

@TomMarius
MST also supports dynamic default values. So things like this can be ambiguous between a view or a property with default. It also breaks the TS type system.

class Foo {
    bar: string = () => Math.random().toString(36);
}
Was this page helpful?
0 / 5 - 0 ratings