Skip to content

Wrap immutable.d.ts type declarations in a module to avoid a global declaration #1869

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

Closed
wants to merge 1 commit into from
Closed

Wrap immutable.d.ts type declarations in a module to avoid a global declaration #1869

wants to merge 1 commit into from

Conversation

vilemj-Viclick
Copy link

The problem:

When using the type-definitions, you provide right now, this is possible and does not get detected not even by running tsc:
image
Notice, there's no import statement. When the code is run, it errors out, unless ProvidePlugin is used in WebPack to add the necessary import.

This is due to the global nature of the namespace declaration.

The solution:

Take the namespace declaration out of the global.
With the type definitions proposed in the pull request this happens:
image
Despite the form of import being proposed when imported the prefered way, it works just fine.
image

Tests pass as far as they can on a windows machine. Your npm scripts for build:prepare are not windows compatible as the shebang notation has no effect on windows. But maybe adding the shebang to the package.json directly, resulting in "build:prepare": "sh -e ./resources/prepare-dist.sh" might help. Most unix-based devices have alias for "sh" to the preferred shell. Be it ksh on a mac or bash on linux machines.

@jdeniau
Copy link
Member

jdeniau commented Jul 29, 2021

Hi !

Can you check that #1854 does not fix this issue ? It is not releases for now but @leebyron made some work that seems to be related.

Thanks

@vilemj-Viclick
Copy link
Author

Hi. Thanks for looking at this.

There is still the global declaration. This causes typescript to be fine with it:
image
Just the IDE hinting at a missing import. But it compiles and does not run.
But it's definitely better than the released version. I just sought to make it another notch better.

@leebyron
Copy link
Collaborator

The current definition file having a global namespace export is intentional since Immutable is a UMD package, and can be used as a global Immutable variable.

The alternative would be to host two separate definition files for the two different use cases, but this single file pattern is fairly common across definitely-typed. For example the React definitions use this same pattern.

@vilemj-Viclick
Copy link
Author

Alright then. I did not study the problem of module type definitions very much. I see you did a good job on the types and surely have your reasons.
All I know is, prop-types for example does not have this problem. I did not check React. But it might be, that prop-types is not a UMD package.

I suspected it was done this way for a reason. Well anyway... Question is, how big of a crowd is the Typescript + UMD crowd?
I'm not saying it should not be UMD on JS level. I'm questioning the necessity to reflect the global UMD variable in typescript type definitions as I wouldn't even know how to use typescript with UMD. Babel/Webpack uses ESM. But you @leebyron seem to be on top of things. You may know how much this is required.

Well, I suspect this is as much as I can contribute. Whatever you decide, I don't think further discussion (me pushing my way) would be productive. Thanks for considering my proposal.

And thanks for taking care of the package! You, guys (or whatever, one never knows), rock!

@bdurrer
Copy link
Contributor

bdurrer commented Nov 3, 2021

As mentioned in slack, maybe it is worth testing the same-name pattern instead of the "types" definitions in the package.json.
That would allow to have multiple definitions for different entry points.

Place separate definitions [name].d.ts besides the umd and es6 scripts and it should be automatically picked up. Maybe it only works when working with folders (e.g. /dist/es6/ and /dist/umd/) or when directly referencing it (e.g. import { List } from 'immutable/umd'). I haven't tested it, but might be worth checking!

@angular/material uses this pattern

@vilemj-Viclick vilemj-Viclick closed this by deleting the head repository Jan 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants