Skip to content

types.compose does not override base properties for snapshot requirements. #2253

Open
@kylemeenehan

Description

@kylemeenehan

Bug report

  • I've checked documentation and searched for existing issues and discussions
  • I've made sure my project is based on the latest MST version
  • Fork this code sandbox or another minimal reproduction.

Sandbox link or minimal reproduction code

https://codesandbox.io/p/sandbox/dpzr23

Describe the expected behavior

When using types.compose, I would expect a child's properties to override its parent's properties. This seemed to work fine in previous versions of MST, but it doesn't seem to be the case for the latest version.

Describe the observed behavior

Given the following code:

import { types } from "mobx-state-tree";

enum Invariant {
    A = 'A',
    B = 'B'
}

const Base = types.model({
    name: types.string,
    invariant: types.enumeration(Object.values(Invariant))
});

const TypeA = types.compose(Base, types.model('TypeA', {
    invariant: Invariant.A
}));

const TypeB = types.compose(Base, types.model('TypeB', {
    invariant: types.literal(Invariant.B)
}));

const a = TypeA.create({
    name: 'Such a great name'
});

const b = TypeB.create({
    name: 'An even better name'
});

I would expect the invariant property to not be required when creating an instance of TypeA or TypeB. However, TypeScript complains and says that the snapshots provided to TypeA.create and TypeB.create are not valid.

Here is the error taken from the codesandbox reproduction:

Argument of type '{ name: string; }' is not assignable to parameter of type 'string | ModelCreationType<{ name: string; invariant: string; }> | undefined'.
  Type '{ name: string; }' is not assignable to type 'ModelCreationType<{ name: string; invariant: string; }>'.
    Property 'invariant' is missing in type '{ name: string; }' but required in type '{ name: string; invariant: string; }'.typescript(2345)

As I say above, I think this is a regression. However, please let me know if I've missed any recent changes in design decisions or misinterpreted any documentation.

Thanks for a great library! If I have some time I might try look into this, but I'm a bit swamped at the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs or examplesDocumentation or examples relatedhelp/PR welcomeHelp/Pull request from contributors to fix the issue is welcomelevel: easy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions