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

Optional default gets a Typescript complaint #1308

Closed
3 tasks done
bryanstearns opened this issue Jun 5, 2019 · 7 comments
Closed
3 tasks done

Optional default gets a Typescript complaint #1308

bryanstearns opened this issue Jun 5, 2019 · 7 comments
Labels
Typescript Issue related to Typescript typings

Comments

@bryanstearns
Copy link

bryanstearns commented Jun 5, 2019

Bug report

When declaring an optional default for a model with entirely defaultable properties, I get a typescript error that I haven't been able to figure out.

  • I've checked documentation and searched for existing issues
  • 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/s/optionaldefaultproblem-qx08w

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

export const ChildModel = types.model("Child").props({
  flag: types.maybe(types.boolean)
});

export const StoreModel = types.model("Store").props({
  child: types.optional(ChildModel, {}) // red squiggly under `{}` with error below
});

Describe the expected behavior
Before I discovered this problem when upgrading a project from much older mobx (4.2.1) & mobx-state-tree (2.0.5) versions, this worked: I could initialize MST models with naked-object snapshots and leave out maybe or optional properties if I wanted.

Describe the observed behavior
If I don't specify values for all the properties when building the snapshot object to use as the default, Typescript complains:

Argument of type '{}' is not assignable to parameter of type 'OptionalDefaultValueOrFunction<IModelType<{ flag: IMaybe<ISimpleType<boolean>>; }, {}, _NotCustomized, _NotCustomized>>'.
  Type '{}' is not assignable to type '() => ExtractCSTWithSTN<IModelType<{ flag: IMaybe<ISimpleType<boolean>>; }, {}, _NotCustomized, _NotCustomized>>'.
    Type '{}' provides no match for the signature '(): ExtractCSTWithSTN<IModelType<{ flag: IMaybe<ISimpleType<boolean>>; }, {}, _NotCustomized, _NotCustomized>>'.ts(2345)

I get essentially the same error if I define ChildModel's flag as types.optional(types.boolean, false) (or just false, which is the same thing, right?).
If I do child: types.optional(ChildModel, { aProp: false }) instead (with either declaration, optional or maybe), the error goes away.

@xaviergonz
Copy link
Contributor

Might be related to this?
#1269

@xaviergonz xaviergonz added bug Confirmed bug Typescript Issue related to Typescript typings labels Jun 5, 2019
@kresli
Copy link

kresli commented Jul 1, 2019

I think its related to #1307

@mweststrate
Copy link
Member

@bryanstearns, please check your tsconfig. The compiler flags should be like:

{
    "strict": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitAny": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitThis": true
}

@mweststrate mweststrate removed the bug Confirmed bug label Jul 13, 2019
@mweststrate
Copy link
Member

(please note that sandbox might not pick up compiler flag changes from my experience, so best test it locally or on stackblitz)

@alex-shamshurin
Copy link

Any update on this ?

@thegedge
Copy link
Collaborator

thegedge commented Jul 7, 2024

I'm not seeing this locally

CleanShot 2024-07-06 at 14 19 42@2x

A lot has changed in the TS definitions for MST lately, along with TS upgrades. Let me know if anyone is still seeing this and, if so, the full set of relevant configs.

@coolsoftwaretyler
Copy link
Collaborator

Thanks for the follow up @thegedge! I do not see this error on v6 either, so I'm going to close it out.

For everyone else in the thread, sorry this has stayed open for so long, although we have definitely had a fix out for a little while.

If y'all need anything else, we can always reopen. Have a great day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typescript Issue related to Typescript typings
Projects
None yet
Development

No branches or pull requests

7 participants