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

[types] Failed to import new types #273

Closed
vitoyucepi opened this issue Dec 10, 2021 · 3 comments · Fixed by #277
Closed

[types] Failed to import new types #273

vitoyucepi opened this issue Dec 10, 2021 · 3 comments · Fixed by #277

Comments

@vitoyucepi
Copy link

vitoyucepi commented Dec 10, 2021

Bug report

Since 0acff90 compression-webpack-plugin provides it's own types and @types/compression-webpack-plugin has marked as stub.
But it's unable to simply migrate from one to another because types can't be imported in the same way.

Actual Behavior

With compression-webpack-plugin@9.1.1 types can't be imported

import {Rule} from 'compression-webpack-plugin'

because

index.ts:1:9 - error TS2305: Module '"compression-webpack-plugin"' has no exported member 'Rule'.

Expected Behavior

Using @types/compression-webpack-plugin@9.0.0 and compression-webpack-plugin@9.1.0 it's possible to write

import {Rule} from 'compression-webpack-plugin'

How Do We Reproduce?

  1. yarn init, use defaults
  2. yarn add typescript
  3. yarn tsc --init, use defaults
  4. yarn add webpack
  5. yarn add compression-webpack-plugin
  6. Create index.ts
    import {Rule} from 'compression-webpack-plugin';
    
    const a: Rule = 'a';
    console.log(a)
  7. yarn tsc

Please paste the results of npx webpack-cli info here, and mention other relevant information

Not related

@alexander-akait
Copy link
Member

alexander-akait commented Dec 11, 2021

Yes, I see, we don't write types manually, they are generated by typescript, and plugin in common js format, so you need to use if you want to import plugin and types:

import CompressionPlugin = require("compression-webpack-plugin");
import type { Rule } from "compression-webpack-plugin/types";
// import { type Rule } from "compression-webpack-plugin/types";

const a: Rule = /test/;

console.log(CompressionPlugin)
console.log(a)

@vitoyucepi
Copy link
Author

Just done some more testing.

-  "types": "types/cjs.d.ts"
+  "types": "types/index.d.ts"

With default typescript project config this works fine.
Here's my script

import CompressionPlugin, {Rule} from 'compression-webpack-plugin';

const a: Rule = 'a';
const b = new CompressionPlugin();
console.log(a)
console.log(b)

Everything else remains the same.

tsconfig.json option esModuleInterop can be set to true for compatibility with the older node versions.

@alexander-akait could you please share a test case that will not work without reexport to cjs.
Is it related to linking and type generation with other projects of yours?

P.S.
I changed the last step in original issue

- yarn tsc index.ts
+ yarn tsc

@alexander-akait
Copy link
Member

Yep, we have a problem, we are in cjs, but exports esm types, due babel and traspiling, we should fix it, so I keep it open

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