-
Notifications
You must be signed in to change notification settings - Fork 642
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
fixes for TS types and added types to unit tests #937
Conversation
There's one unit test failing now that wasn't failing before adding TypeFlags to exports):
|
Apparently the error was an accidental change to mobx-state-tree package-json that changed mobx version to 4.3.1 from 5.0.3. |
besides making TypeFlags public and exported also added TypeFlags imports to undo-manager and the other mst-middleware to fix #930 |
Also fixes #932 |
just added a fix (with unit test) for optional enums inside a model crashing
because types.optional would inherit as flag TypeFlags.Union, but not "types" and therefore crash here
so I fixed it like this
another option would be to make optional just return the optional flag, but I'm not sure if that's better |
Thanks for the investigation! I think the solution is correct, will double
check next week!
Op vr 20 jul. 2018 om 20:01 schreef Javier Gonzalez <
[email protected]>:
… just added a fix (with unit test) for optional enums inside a model
crashing
in other words, the following will crash:
test("should support optional enums inside a model", () => {
const TrafficLight = types.model({
color: types.optional(types.enumeration(["Red", "Orange", "Green"]), "Orange")
})
const l = TrafficLight.create({})
expect(l.color).toBe("Orange")
})
because types.optional would inherit as flag TypeFlags.Union, but not
"types" and therefore crash here
function tryGetOptional(type: any): OptionalValue<any, any, any> | undefined {
if (!type) return undefined
if (type.flags & TypeFlags.Union) return type.types.find(tryGetOptional) // THIS LINE
if (type.flags & TypeFlags.Late) return tryGetOptional(type.subType)
if (type.flags & TypeFlags.Optional) return type
return undefined
}
so I fixed it like this
function tryGetOptional(type: any): OptionalValue<any, any, any> | undefined {
if (!type) return undefined
// we need to check for type.types since an optional union doesn't have direct subtypes
if (type.flags & TypeFlags.Union && type.types) return type.types.find(tryGetOptional)
if (type.flags & TypeFlags.Late && type.subType) return tryGetOptional(type.subType)
if (type.flags & TypeFlags.Optional) return type
return undefined
}
another option would be to make optional just return the optional flag,
but I'm not sure if that's better
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#937 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhG2ySiJCLS3LoSoN1meNrL0Wjogaks5uIhsVgaJpZM4VXR1D>
.
|
Maybe fixes #922 |
Ok, basically I took the opportunity to make a revamp of the typescript typing to further improve it. With these changes now getSnapshot is properly typed without the need to use its generic argument, references and identifiers work properly and lots of other little fixes. While there are some small breaking changes about typings, the core API is not changed at all. |
I made this available in npm as |
I had to remove DeepImmutable. While cool (made snapshots from getSnapshot unmodifiable, etc) I have a large project that uses MST and it was getting out of ram errors while compiling because of it. That being said, I'm quite happy with the current state of the typings 👍 so I don't think I'll make further changes. |
@xaviergonz Thanks for the awesome work! Will play a bit with and release |
@mweststrate my pleasure :) |
Took a close look and these are great improvements! Really awesome work. I've just send you a maintainers invite, that will simplify the workflow for any future contributions a lot, as you can directly push, branch etc on this repo :-)! |
@xaviergonz thanks this fixes my issue 👍 |
cool thx - tried it out (v3.0.2) looks like it's causing some problems with fork-ts-checker-webpack-plugin - "Starting type checking and linting service..." will never finish and creating a node instance with >100% cpu went back to 2.2.0 (working) Typescript 2.9 |
and if you disable tslint?
Op do 26 jul. 2018 om 16:04 schreef Marvin <[email protected]>:
… cool thx - tried it out (v3.0.2)
looks like it's causing some problems with fork-ts-checker-webpack-plugin
- "Starting type checking and linting service..." will never finish and
creating a node instance with >100% cpu
went back to 2.2.0 (working)
updated to 3.0.0 (working)
updated to 3.0.1 (not working)
Typescript 2.9
tslint 5.11.0
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#937 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhPzuhFVPpsRD7snAf5q4Y4rEJgT3ks5uKcxxgaJpZM4VXR1D>
.
|
I have seen that happen from time to time when there are lots of compilation errors. Basically the default node memory of node processes is around 512mb and complex type checking takes lots of ram and then crash. Things I'd try include:
then using ISomeModelCreation, ISomeModelSnapshot, ISomeModel or I guess TS could get their act together and optimize that :) |
import { types, getSnapshot } from "mobx-state-tree";
const Square = types
.model(
"Square",
{
width: types.number
}
)
const a = getSnapshot(Square) I'm getting this error for the getSnapshot call I'm probably missing something :) |
Yes, you are snapshoitting a type, not an instance
Op do 26 jul. 2018 20:38 schreef Bnaya Peretz <[email protected]>:
… import { types, getSnapshot } from "mobx-state-tree";
const Square = types
.model(
"Square",
{
width: types.number
}
)
const a = getSnapshot(Square)
I'm getting this error for the getSnapshot call
Type 'IModelType<ModelPropertiesDeclarationToProperties<{ width:
ISimpleType<number>; }>, {}, ModelCrea...' has no properties in common with
type 'IStateTreeNode<any, {}>'.
I'm probably missing something :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#937 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhIT0NtIxj1xFFgDcdTxrINOzVkhdks5uKgzBgaJpZM4VXR1D>
.
|
Of Course😂 thanks
…On Thu, Jul 26, 2018, 21:43 Michel Weststrate ***@***.***> wrote:
Yes, you are snapshoitting a type, not an instance
Op do 26 jul. 2018 20:38 schreef Bnaya Peretz ***@***.***>:
> import { types, getSnapshot } from "mobx-state-tree";
> const Square = types
> .model(
> "Square",
> {
> width: types.number
> }
> )
> const a = getSnapshot(Square)
>
> I'm getting this error for the getSnapshot call
> Type 'IModelType<ModelPropertiesDeclarationToProperties<{ width:
> ISimpleType<number>; }>, {}, ModelCrea...' has no properties in common
with
> type 'IStateTreeNode<any, {}>'.
>
> I'm probably missing something :)
>
> —
> You are receiving this because you were mentioned.
>
>
> Reply to this email directly, view it on GitHub
> <
#937 (comment)
>,
> or mute the thread
> <
https://github.com/notifications/unsubscribe-auth/ABvGhIT0NtIxj1xFFgDcdTxrINOzVkhdks5uKgzBgaJpZM4VXR1D
>
> .
>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#937 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABPpHuNjGJhGVxQ-6E5G6EqUhyb_Fadxks5uKg3BgaJpZM4VXR1D>
.
|
@xaviergonz I tried around, but still not helping. The strange thing is that it is working with 3.0.0. I used that version to remove all lint errors which popped up through the breaking changes. then went back to 3.0.2 - still not working (i mean it is compiling, but the linter is not working) I try to play around with it a little bit and try to find out which commit breaks it. |
so the compiler works but the linter doesn't? what if you just run tslint stand alone? |
Sounds more like an issue to report at the linter library?
Op vr 27 jul. 2018 11:26 schreef Javier Gonzalez <[email protected]>:
… so the compiler works but the linter doesn't? what if you just run tslint
stand alone?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#937 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhJXOdJN90I1WB0qPKfD_8awc_5v3ks5uKtzdgaJpZM4VXR1D>
.
|
kind of yes =/
working fine in 3.0.0 and does nothing in 3.0.1 |
A (kinda big) pull request to fix lots of TS typing issues
Oh, and also added types to all tests
As a plus I publicly exported TestFlags so mst-middlewares won't complain that it can't find them.
A couple of important things:
Fixes: #923 #930 #932 - maybe #922
Btw, shouldn't custom type actually use <C, S, T> rather than <S|T,S,T> for more flexibility?