Skip to content

TS Error with types.optional in 3.0.0 #923

@srolel

Description

@srolel
  • I think something is not working as it should.
    • I've checked documentation and searched for existing issues
    • I've made sure your project is based on the latest MST version
    • Fork this code sandbox or another minimal reproduction.
    • Describe expected behavior
    • Describe observed behavior

The error doesn't seem to show in codesandbox (no type errors seem to show) but I made a repo reproducing the issue. Using mobx@5.0.3, mobx-state-tree@3.0.0, typescript@2.9.2 and this file:

import { types } from 'mobx-state-tree'

const Foo = types.model('Foo', {
    name: types.optional(types.string, ''),
});

export const Bar = types.model('Bar', {
    foos: types.optional(types.array(Foo), []),
});

types.optional(types.map(Bar), {});

Removing any of the types.optional makes the error go away.

Expected behavior - no errors.

Observed behavior - this lovely error on the last line:

index.ts:11:16 - error TS2345: Argument of type 'IComplexType<IKeyValueMap<ModelCreationType<ModelPropertiesDeclarationToProperties<{ foos: IType<...' is not assignable to parameter of type 'IType<IKeyValueMap<ModelCreationType<ModelPropertiesDeclarationToProperties<{ foos: IType<Readonl...'.
  Types of property 'Type' are incompatible.
    Type 'import("C:/repos/mobx-typescript-error/node_modules/mobx-state-tree/dist/types/complex-types/map"...' is not assignable to type 'import("C:/repos/mobx-typescript-error/node_modules/mobx-state-tree/dist/types/complex-types/map"...'. Two different types with this name exist, but they are unrelated.
      Type 'IMSTMap<ModelCreationType<ModelPropertiesDeclarationToProperties<{ foos: IType<ReadonlyArray<Mode...' is not assignable to type '{ toJSON?(): IKeyValueMap<ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ foos: IType<...'.
        Types of property 'toJSON' are incompatible.
          Type '() => IKeyValueMap<ModelInstanceType<ModelPropertiesDeclarationToProperties<{ foos: IType<Readonl...' is not assignable to type '() => IKeyValueMap<ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ foos: IType<Readonl...'.
            Type 'IKeyValueMap<ModelInstanceType<ModelPropertiesDeclarationToProperties<{ foos: IType<ReadonlyArray...' is not assignable to type 'IKeyValueMap<ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ foos: IType<ReadonlyArray...'.
              Type 'ModelInstanceType<ModelPropertiesDeclarationToProperties<{ foos: IType<ReadonlyArray<ModelCreatio...' is not assignable to type 'ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ foos: IType<ReadonlyArray<ModelCreatio...'.
                Type 'ModelInstanceType<ModelPropertiesDeclarationToProperties<{ foos: IType<ReadonlyArray<ModelCreatio...' is not assignable to type '{ readonly foos: ReadonlyArray<ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ name: I...'.
                  Types of property 'foos' are incompatible.
                    Type '(IObservableArray<ModelCreationType<ModelPropertiesDeclarationToProperties<{ name: IType<string, ...' is not assignable to type 'ReadonlyArray<ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ name: IType<string, stri...'.
                      Type 'IObservableArray<ModelCreationType<ModelPropertiesDeclarationToProperties<{ name: IType<string, s...' is not assignable to type 'ReadonlyArray<ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ name: IType<string, stri...'.
                        Types of property 'concat' are incompatible.
                          Type '{ (...items: ConcatArray<ModelCreationType<ModelPropertiesDeclarationToProperties<{ name: IType<s...' is not assignable to type '{ (...items: ConcatArray<ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ name: IType<s...'.
                            Type 'ModelCreationType<ModelPropertiesDeclarationToProperties<{ name: IType<string, string, string> & ...' is not assignable to type 'ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ name: IType<string, string, string> & ...'.
                              Type 'ModelCreationType<ModelPropertiesDeclarationToProperties<{ name: IType<string, string, string> & ...' is not assignable to type 'ModelSnapshotType<ModelPropertiesDeclarationToProperties<{ name: IType<string, string, string> & ...'.
                                Type 'ModelCreationType<ModelPropertiesDeclarationToProperties<{ name: IType<string, string, string> & ...' is not assignable to type '{ readonly name: string; }'.
                                  Property 'name' is optional in type 'ModelCreationType<ModelPropertiesDeclarationToProperties<{ name: IType<string, string, string> & ...' but required in type '{

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions