mobx-state-tree
mobx-state-tree copied to clipboard
strictNullChecks: false does not get along well with types.optional in Typescript
Bug report
- [x] I've checked documentation and searched for existing issues
- [x] I've made sure my project is based on the latest MST version
- [x] Fork this code sandbox or another minimal reproduction.
Sandbox link or minimal reproduction code https://codesandbox.io/s/zn8262jl0l
Describe the expected behavior There should be no errors in typescript
Describe the observed behavior
This not helpful error appears

This happens since version v3.11.0 where the optional type was revisited adding a third argument.
However typescript gets angry when that default [undefined] value is not explicitly specified, making your life impossible when using primitive types (which are syntactic sugar to t.optional).
E.g. isModalOpen: false in a model.
This is probably related to #1208
Could you post your tsconfig.json? Do you have strict null checks disabled by any chance?
Yes, this is my tsconfig.json, I have strict null checks disabled, however I don't think MST must depend on my personal typescript configuration.
{
"compilerOptions": {
"target": "es5",
"lib": ["esnext", "dom", "dom.iterable"],
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"jsx": "react",
"strict": true,
"strictNullChecks": false,
"noImplicitReturns": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": false,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true
},
"include": ["src"]
}
I'm no longer able to reproduce (on master).
Let me know if you're still experiencing an issue. Otherwise, we'll close this one out :)
Yup, looks like the reproduction Sandbox has it resolved on mobx-state-tree@latest: https://codesandbox.io/p/sandbox/zn8262jl0l?file=%2FState.tsx%3A7%2C4.
Closin' out. Thanks for the follow up on this @thegedge, and sorry to everyone else that it took a while to fix and close.