-
-
Notifications
You must be signed in to change notification settings - Fork 852
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
Version 1.9.1 Typescript issue with generics #272
Comments
Thank you for finding these edge cases! Need to improve the typescript tests, I guess. |
Interestingly, when you explicitly type the
|
I changed the code of
New:
This gives a new error message:
It looks like it chooses the right conditional statement. And with a last small change to my code (
New:
Of course this change shouldn't be necessary here. Maybe you can figure out how to solve this within Immer. |
What typescript version are you using? Changing the |
according to package.json:
But my tsconfig.json is a bit messy, because it's a legacy project with lot's of js and a bit of ts code. And a mixture of AngularJS (the old one) with decorators, and React (latest version):
|
Using typescript 3.1.4, I'm getting |
Also you probably want to do |
Using typescript 3.2.2, using your |
I've now tried 3.1.4 There I also get the Here's TypeScript's
3.2.2:
Looks the same. Maybe it's something from my Now back at
My Btw: I didn't check the TypeScript console output. I just looked at VSCode. Let me recheck... I'll record a small video :D EDIT: The video: http://sendvid.com/85i91zpl In the console you can see |
Sorry, I was using a locally-linked version, which had some changes I made for #273. 😅 Still trying to figure this out.. Maybe it's just a bug with Typescript itself? |
Yes, maybe it's a TypeScript issue... I now created a completely blank new TS project.
tsconfig.json
index.ts
immer.d.ts
Compile using:
Still the same behavior: When changing EDIT: I now made another change to your TypeScript definitions:
Note: Even this does not work:
With this I don't need The problem seems to be either within EDIT 2: Maybe I found a solution (???) I only changed
Now all errors are gone. But I'm not sure if this is "good". |
If you want to look into this more, you should..
|
@benneq Your last change to |
Includes: - a much improved DraftArray<T> - an "extends object" constraint added to DraftObject<T> - a relaxed DraftTuple<T> constraint (tuples cannot be readonly) Fixes #272
Alright, I'm counting this as a TypeScript bug. The best workaround is: import produce from 'immer';
export function insertAtIndex<T>(array: ReadonlyArray<T>, index: number, elem: T): ReadonlyArray<T>
export function insertAtIndex(array: ReadonlyArray<any>, index: number, elem: any) {
return produce(array, draft => {
draft.splice(index, 0, elem);
});
} |
@benneq could you setup a small TS based code sandbox (or some other online tool), to fiddle around more easily? I suspect we might be running in a TS limitation here, no be able to expre co- versus contra- variance in generics. |
Hi, I just downloaded you Stay tuned! EDIT: I now searched the internet for "typescript deep immutable" to look how people have done this. (Basically the opposite of "Draft"). Then I tried those code snippets, and most didn't work with generics. But then I found a few that worked. And now things become pretty clear! In your typings you use The working examples for Now I try to build a small test case and see if it really works... EDIT 2: I don't get it working :( How about creating an issue within the TypeScript project? But I don't know what to write, because I'm totally unsure where the problem is... EDIT 3: I think I at least now understand the underlying problem...
EDIT 4: In the meantime I found some other way to create recursive models in TypeScript: |
@mweststrate The @benneq I haven't read your diagnosis yet, but that last link looks like a good road to take after v1.9.3. |
My diagnonis can be summarized in 1 word: useless :D
Yes, maybe that‘s a better approach, but I think it won‘t fix the problem with generics.
… Am 18.12.2018 um 14:21 schrieb Alec Larson ***@***.***>:
@mweststrate The next branch already contains a test for this. So far, the issue seems to be that Draft<T> is not assignable to T.
@benneq I haven't read your diagnosis yet, but that last link looks like a good road to take after v1.9.3.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
And other small improvements. Fixes #272
Okay, the best workaround (explicit argument types) is demonstrated here: Here are some related tests: |
Thanks for the assistance, @benneq! ❤️ |
🎉 This issue has been resolved in version 1.9.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Wow. Thank you. But I expect that things like |
@benneq Let me know in a new issue if you hit anything. :) |
Hello again :D (hope, it doesn't get annoying)
I've written a small function:
Without using
as any
I get:This error only appears within this generic function in combination with Immer.
These two cases work as expected:
The text was updated successfully, but these errors were encountered: