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

Compilation errors in TypeScript ES5-only project (+ workaround) #321

Closed
2 of 3 tasks
bard opened this issue Mar 3, 2019 · 2 comments
Closed
2 of 3 tasks

Compilation errors in TypeScript ES5-only project (+ workaround) #321

bard opened this issue Mar 3, 2019 · 2 comments

Comments

@bard
Copy link

bard commented Mar 3, 2019

  • Issue: in project targeting only ES5, using immer causes type errors at compilation time
    • Version: 2.1.0
    • SIMPLE Reproduction: https://github.com/bard/repro-immer-typescript-es5
    • Expected behavior: file importing immer compiles cleanly
    • Observed behavior: file importing immer triggers compilation errors
    • Occurs when using Proxies (use setUseProxies(true))
    • Occurs in the ES5 implementation (use setUseProxies(false))

Context: I'm working on a TypeScript project where ES5 compatibility needs to be ensured and bundle size must be limited, so specifying lib: ["es2015"] is not an option (it would allow code that uses ES2015 classes to slip through) nor it is an option to polyfill everything just so es2015 features would work if accidentally introduced. I understand from the README that immer is also intended for ES5-only environments.

Currently, when lib: ["es5"] is in tsconfig.json, compiling a TypeScript file that contains nothing more than import produce from 'immer' results in the following errors (run yarn run tsc in the reproduction repository):

$ /tmp/repro/node_modules/.bin/tsc
node_modules/immer/dist/immer.d.ts:4:7 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

4     | Map<any, any>
        ~~~

node_modules/immer/dist/immer.d.ts:5:7 - error TS2583: Cannot find name 'WeakMap'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

5     | WeakMap<any, any>
        ~~~~~~~

node_modules/immer/dist/immer.d.ts:6:7 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

6     | Set<any>
        ~~~

node_modules/immer/dist/immer.d.ts:7:7 - error TS2583: Cannot find name 'WeakSet'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

7     | WeakSet<any>
        ~~~~~~~


Found 4 errors.

error Command failed with exit code 2.

A workaround I'm using is to place the following in my local declarations (rename typings.d.text to typings.d.ts in the reproduction repository):

// typings.d.ts
declare interface Map<K, V> {}
declare interface WeakMap<K, V> {}
declare interface WeakSet<K> {}
declare interface Set<K> {}

This silences errors caused by importing immer, without letting one use those classes accidentally. If one types something like "const m = new Map()" one gets error TS2693: 'Map' only refers to a type, but is being used as a value here..

Thanks for immer, by the way, exceptional work!

@aleclarson
Copy link
Member

Thanks for the workaround! 👍

@mweststrate Can we do anything to avoid this issue? I think not.

aleclarson added a commit that referenced this issue Mar 10, 2019
I noticed that "@types/lodash" uses this declaration for ES5 compatibility.

Fixes #321
aleclarson added a commit that referenced this issue Mar 12, 2019
I noticed that "@types/lodash" uses this declaration for ES5 compatibility.

Fixes #321
@aleclarson
Copy link
Member

🎉 This issue has been resolved in version 2.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants