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

Typescript Examples #9

Closed
mikecann opened this issue Dec 11, 2016 · 10 comments
Closed

Typescript Examples #9

mikecann opened this issue Dec 11, 2016 · 10 comments

Comments

@mikecann
Copy link
Contributor

I know @mweststrate mentioned in this issue: https://github.com/mobxjs/mobx/issues that better typescript support will be coming soon but I thought I would just start this issue to track my my definite interest in better typing support 😄

@mquandalle
Copy link

So in Typescript, the current createFactory example doesn't work because this implicitly has type any in the methods body. Is anyone aware of a way to write models in Typescript? (I do understand that this project is a work in progress, I'm just curious if someone has found a way, even hacky, to experiment with it in Typescript)

@mweststrate
Copy link
Member

mweststrate commented Jan 31, 2017 via email

@mquandalle
Copy link

Thank you, I didn't know this "typing this" feature. I haven't found a way to use the types directly defined in the base model but manually creating an interface is working just fine.

@bradenhs
Copy link

The following looks like it's scheduled for the May release of typescript and I'm thinking it should help a lot with this issue: microsoft/TypeScript#14141

@mquandalle
Copy link

The TypeScript PR implementing this typing has been merged and is now usable using typescript@next on NPM.

@Bnaya
Copy link
Member

Bnaya commented Mar 22, 2017

With this keyword there are other issues
use this code

import {
  autorun,
  observable,
  reaction,
} from "mobx";
import {
  action,
  createFactory,
  types,
} from "mobx-state-tree";

class ModelSchema {
  public completed = false;
  public id = types.string("");
  public text = types.string("");
  public toggleCompleted = action(function toggleCompleted (this: ModelSchema, completed: boolean) {
    this.completed = completed;
  });
}

// console.log(new ModelSchema());

const sStatic = {
  completed: false,
  id: types.string(""),
  text: types.string(""),
  toggleCompleted: action(function toggleCompleted (this: typeof sStatic, completed: boolean) {
    this.completed = completed;
  }),
};

const schema = new ModelSchema();

const TodoModelTree = createFactory("TodoModel", schema);
const TodoModelTree2 = createFactory("TodoModel2", sStatic);

const t = TodoModelTree();
const t2 = TodoModelTree2();

reaction(() => {
  return t.completed;
}, function cb() {
  console.log('todo is', t.completed)
});

reaction(() => {
  return t2.completed;
}, function cb() {
  console.log('todo2 is', t2.completed)
});


t.toggleCompleted(true);

t2.toggleCompleted(true);

and see the typescript types error that you get

@mattiamanzati
Copy link
Contributor

Hello!

types.string("")

should be instead

types.string

I am not able to test it now, but that should fix an issue. If you want to provide a default, you can use types.withDefault(types.string, "")

Let me know if this solves the issue!

@mquandalle
Copy link

I haven't tested with TS 1.3-alpha, but I was previously using type casting like "" as string in my models and it was working just fine. Type checking is done at compile time anyway, so why bother using some runtime constructs like types.string for the sole role of type safety?

@mattiamanzati
Copy link
Contributor

Runtime checking is performed mainly in applySnapshot to ensure that the incoming snapshot is valid! :)

mweststrate added a commit that referenced this issue Mar 28, 2017
@mweststrate
Copy link
Member

closing this one in favor of #66 (feel free to reopen if this one doesn't cover all issues)

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

6 participants