Skip to content

Type create/validate to accept an instance type - #2199

Merged
coolsoftwaretyler merged 2 commits into
mobxjs:masterfrom
thegedge:instance-acceptable-for-create
Aug 1, 2024
Merged

coolsoftwaretyler merged 2 commits into
mobxjs:masterfrom
thegedge:instance-acceptable-for-create

Conversation

@thegedge

Copy link
Copy Markdown
Collaborator

What does this PR do and why?

We've had issues with typings when instantiating references

CleanShot 2024-07-20 at 11 42 42@2x

#1568 for example, and maybe even #1764.

This PR resolves that by typing create to accept an instance type. I've added a "kitchen sink" test for create to test both snapshots and instances.

Steps to validate locally

The test added in this PR will type issues (like in above the screenshot) if you revert 6176e94.

@thegedge

thegedge commented Jul 20, 2024 •

Copy link
Copy Markdown
Collaborator Author

Hold off on a final review. I realized our tsconfig isn't properly typechecking tests, and this introduces a typing bug: it allows passing in a value for a view in model types.

Technically this works (it just gets ignored) but I'm thinking ideally we make it fail typechecking so users avoid making that error. This is actually tough to do, but I think we can do this by marking the getters as readonly and excluding those from the instance type in create.

I'm going to play with that idea, and if it works out, great! If it doesn't, I think I may remove the one test that's failing:

CleanShot 2024-07-20 at 12 16 39@2x

Do let me know if you think the tests I've added are comprehensive enough though! Want to make sure we have confidence in this change.

@thegedge
thegedge force-pushed the instance-acceptable-for-create branch from 6176e94 to e1b5068 Compare July 20, 2024 16:37
@thegedge

Copy link
Copy Markdown
Collaborator Author

Will rebase this after #2200 merged, which should fix the typechecking / build stuff

Comment thread src/core/type/type.ts
@coolsoftwaretyler

Copy link
Copy Markdown
Collaborator

Hey @thegedge - just merged #2200 so you should be good to rebase and then figure out the new typing issues. Let me know when this is ready for another look.

@thegedge
thegedge force-pushed the instance-acceptable-for-create branch from e1b5068 to f893791 Compare July 31, 2024 15:35
@thegedge

Copy link
Copy Markdown
Collaborator Author

@coolsoftwaretyler this is good for review now! <3

I was thinking we could also get this one in for #2201

@coolsoftwaretyler

Copy link
Copy Markdown
Collaborator

Perfect! Great idea. Will take a look sometime this week and get this into the next preview release.

@coolsoftwaretyler

Copy link
Copy Markdown
Collaborator

Alright, looks excellent and we will get this into the v7 preview. I think we'll consider this a breaking change, even though it should mostly be backwards compatible. I think type changes are just best treated as breaks overall, in case people are relying on the old type behavior.

@coolsoftwaretyler
coolsoftwaretyler merged commit 3a2945d into mobxjs:master Aug 1, 2024
@thegedge
thegedge deleted the instance-acceptable-for-create branch November 9, 2024 16:08
coolsoftwaretyler added a commit to coolsoftwaretyler/mobx-state-tree that referenced this pull request Oct 2, 2026
- mobxjs#1458 REPRODUCES: safeReference invalidates when an old subtree with
  the same id is destroyed; a guard test shows a skip-only fix is not
  enough, the watcher must move to the node the id now resolves to.
- mobxjs#1778 REPRODUCES (types): resolveIdentifier's type parameter rejects
  snapshotProcessor types.
- mobxjs#1760 ALREADY_FIXED by mobxjs#2199.
- mobxjs#1291 CANNOT_REPRODUCE and likely obsolete: the quoted error is the
  MobX 4/5 computationDepth invariant, which MobX 7 no longer has.
- mobxjs#2255 CANNOT_REPRODUCE: thread has no runnable code; blockers listed.

Also records the fixing commits for the earlier mobxjs#1297 (mobxjs#1581), mobxjs#1738
and mobxjs#1640 (mobxjs#2199) tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
coolsoftwaretyler added a commit to coolsoftwaretyler/mobx-state-tree that referenced this pull request Oct 2, 2026
All six reproduce; each file compiles today and typecheck fails when
the defect is fixed, via @ts-expect-error BUG markers.

- mobxjs#1403, mobxjs#2216, mobxjs#2253: props and compose use plain intersections, so an
  optional override of a required prop stays required or becomes never.
  Re-landing an Omit-based override reopens the mobxjs#2230 depth trade-off
  (mobxjs#2218 reverted by mobxjs#2234); compose overloads need the same change.
- mobxjs#1463: create() accepts any object because ExcludeReadonly (from
  mobxjs#2199) is an indexed access over WritableKeys; the fix only tightens.
- mobxjs#1157: one directive is the reported inference limit on an
  unannotated circular view; ten others are the same mobxjs#2199 regression
  and flip when WritableKeys stops being evaluated eagerly.
- mobxjs#1367: .props after preProcessSnapshot never adds new props to the
  declared snapshot type.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants