kciter / react-barcode

A <Barcode/> component for use with React.
http://kciter.github.io/react-barcode/
ISC License
396 stars 72 forks source link

Type support #66

Closed dor1202 closed 2 years ago

dor1202 commented 2 years ago

Is there any types support plan in the future?

JeffreyStevens commented 2 years ago

I had to add this to my types.d.ts to work around the issue:

declare module 'react-barcode';

dor1202 commented 2 years ago

@JeffreyStevens Hi! Adding the declare module 'react-barcode'; doesn't seem to work for me, except creating a types file for the package, do you have more ideas?

dor1202 commented 2 years ago

@JeffreyStevens . It looks like they added types about 6 months ago in This file and based on commit #27c they added the types but the types still not downloading from npm due to missing #64 , so adding only declare module 'react-barcode'; has no effect. Did you add a custom file types? if so can you send it to me?

JeffreyStevens commented 2 years ago

Ok, I was getting this error and the declare fixes it for our Typescript project. We do have a custom type.d.ts file for our project for this purpose.

ERROR in src/components/BarcodeLabel.tsx:2:21
TS7016: Could not find a declaration file for module 'react-barcode'. '/Users/m087610/tfs/STS-Cloud-UI/sts-react/node_modules/react-barcode/lib/react-barcode.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/react-barcode` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-barcode';`
    1 | import React, {FC, MutableRefObject} from 'react';
  > 2 | import Barcode from 'react-barcode';
      |                     ^^^^^^^^^^^^^^^
rtmann commented 2 years ago

This project has types but they are done incorrectly. The file needs to be renamed to index.d.ts instead of react-barcode.d.ts.

Literally just need to rename the react-barcode.d.ts in this project. Typescript types resolution uses a schema like this

So when importing Barcode from 'react-barcode' it can't find the index.d.ts.

fnimick commented 2 years ago

@kciter fyi 1.4.4 doesn't resolve the issue. The problem is that the built package has a malformed types entry in package.json - it refers to src/index.d.ts which doesn't exist in the downloaded package.

I suggest moving index.d.ts to the top level of the built package, which solves the problem. Not sure how to do that with your build system though.