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

Using the Typescript compiler to generate model definitions from classes #136

Closed
erkieh opened this issue May 19, 2017 · 4 comments
Closed
Labels
question Question or help request

Comments

@erkieh
Copy link

erkieh commented May 19, 2017

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

Just some transformer examples:
microsoft/TypeScript#14419 (comment)

@mattiamanzati
Copy link
Contributor

mattiamanzati commented May 19, 2017

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! :)

@mattiamanzati mattiamanzati added the question Question or help request label May 19, 2017
@erkieh
Copy link
Author

erkieh commented May 19, 2017

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.

@erkieh erkieh closed this as completed May 19, 2017
@TomMarius
Copy link

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

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

@mattiamanzati
Copy link
Contributor

@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);
}

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

No branches or pull requests

3 participants