Skip to content

Type 'ISimpleType<string>' is not assignable to type 'string' #2238

Answered by thegedge
OnkelTem asked this question in Q&A
Discussion options

You must be logged in to vote

The typing here is probably a bit confusing, but I'd recommend using your type for instances, not the model itself:

type Foo = {
  foo: string
}

const MyModelFoo = types.model('Foo', {
  foo: types.string,
})

const fooInstance: Foo = MyModelFoo.create({ foo: "test" })

The problem in your example is that the generic argument for types.model describes the structure of the properties and not the instance. { foo: types.string } does not match { foo: string }, hence the error you're seeing. It would match { foo: "abc" }, which would actually be the same as { foo: types.optional(types.string, "abc") }.

In general I'd recommend always letting TypeScript derive the generic arguments for types.…

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@thegedge
Comment options

thegedge Jan 7, 2025
Collaborator

@OnkelTem
Comment options

@thegedge
Comment options

thegedge Jan 7, 2025
Collaborator

@OnkelTem
Comment options

@coolsoftwaretyler
Comment options

Answer selected by coolsoftwaretyler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants