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

Bump typescript from 3.9.10 to 5.3.3 #2146

Merged
merged 1 commit into from
Feb 21, 2024

Conversation

thegedge
Copy link
Collaborator

What does this PR do and why?

Upgrade mobx-state-tree to the latest version of typescript (3.9 -> 5.3).

We were really far behind and missing out on all of the performance improvements and bug fixes, along with features that will allow writing better types for MST (e.g., variadic tuple types for types.union to avoid an IAnyType when 10 or more types unioned).

Steps to validate locally

Leaning on existing tests for build correctness, and other than that I've corrected a few failing typechecks.

The include config in tsconfig.json has also been updated to ensure typechecking in __tests__ happens, and I've added a typecheck command to package.json to be able to run just a typecheck on the codebase. I iterated on those until everything passed.

@@ -16,7 +16,7 @@ import {
joinJsonPath
} from "../../src"

function testPatches<C, S, T>(
function testPatches<C, S, T extends object>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be correct. Without it, getSnapshot is complaining:

Argument of type 'STNValue<T, IType<C, S, T>>' is not assignable to parameter of type 'IStateTreeNode<IType<any, unknown, any>>'.
  Type 'T | (T & IStateTreeNode<IType<C, S, T>>)' is not assignable to type 'IStateTreeNode<IType<any, unknown, any>>'.
    Type 'T' is not assignable to type 'IStateTreeNode<IType<any, unknown, any>>'.ts(2345)
jsonpatch.test.ts(19, 28): This type parameter might need an `extends IStateTreeNode<IType<any, unknown, any>>` constraint.

The problem is that IType["create"] returns an STNValue, and without any constraints you can see that it will be (T & IStateTreeNode<IT>) | T, and getSnapshot needs an IStateTreeNode<IT>.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the note! This makes sense. I think we were hitting that over in #2103

src/core/node/object-node.ts Show resolved Hide resolved
@@ -22,6 +22,10 @@ import {
} from "../../src"
import { $nonEmptyObject } from "../../src/internal"

const assertTypesEqual = <T, U>(t: T, u: Exact<T, U> extends never ? T : U): [T, U] =>
Copy link
Collaborator Author

@thegedge thegedge Feb 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exact<T, U> extends never ? T : U means "when not the exact same type, use the type of the first arg otherwise the type of the second arg". Making it the first arg's type when not exact gives us a typechecking error directly from TS:

TODO: Verify this is still okay if !Exact<T, U> but T extends U
Okay, this needs some work because assertTypesEqual(_ as { a: 1 }, _ as { a: 1; b: 2 }) doesn't fail typechecking. Will fix!

Fixed. I've added a "checker" type to show you which keys aren't matching exactly:

CleanShot 2024-02-17 at 15 21 18@2x

Would have loved to have something like "Mismatched properties: a, b", but unfortunate a union type + template string literals don't work that way 😢

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phenomenal. This is what I really needed when I was first trying to debug the old spec.ts failures. Thank you!

@coolsoftwaretyler coolsoftwaretyler marked this pull request as ready for review February 21, 2024 15:35
@coolsoftwaretyler
Copy link
Collaborator

Once again, @thegedge - thank you very much for taking this on. It will un-block us on #2103, and I hope it will also lead to better typings in general. I'll make sure we stay up to date with TypeScript changes on a more regular cadence from here on out.

I'm going to merge this in. I have a few other PRs I'd like to merge this week alongside it, so I'll ship some pre-releases throughout to make sure the TS changes are all good for our downstream users.

@coolsoftwaretyler coolsoftwaretyler merged commit 0aa42a7 into mobxjs:master Feb 21, 2024
1 check passed
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