magika
magika copied to clipboard
Add typescript type definitions for js lib
Hi,
it would be great if you could add or auto generate type definitions for the js lib so it can be used with typescript.
Since there isn't a CI job to handle automatic releases and I'm not aware of your release process and plans for the lib, I'd rather not myself create a PR for this yet.
facing the same issue : Could not find a declaration file for module 'magika'. '/node_modules/magika/magika.js' implicitly has an 'any' type.
Thanks for reporting, looking into it.
See #129, not sure if it's related.
I tried adding types to DefinitelyTyped to use Magika with TypeScript. However, I encountered the following error, and the tests did not pass. Does Magika not support CommonJS?
A require call resolved to an ESM JavaScript file, which is an error in Node and some bundlers.
CommonJS consumers will need to use a dynamic import.
DefinitelyTyped's pull request is below: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68765
@reyammer It appears that Magica doesn't seem to support CommonJS, as shown in https://github.com/google/magika/issues/129. This suggests that we should be able to add types to DefinitelyTyped.
module.exports = { Magika };
@chris-si I have added type definitions for Magika to DefinitelyTyped. With this, you can now run it in TypeScript without any errors.
npm: https://www.npmjs.com/package/@types/magika
Please execute the following command.
npm install --save-dev @types/magika
or
pnpm add -D @types/magika
Additional information: Magika did not support CommonJS and didn't pass the tests at DefinitelyTyped, the DefinitelyTyped team has been modified to allow the tests to pass even if the library does not support CommonJS.
We've published a version with commonJS support, and the library has been rewritten in Typescript as of yesterday. Our types should be bundled with the package. Please take a look and let me know if they are as expected.
That's great to hear! I'll give it a try in the coming days.
That's great. I tried 0.2.10 out right away.
I'm using Magika as a client application. While doing so, I encountered an issue concerning the argument types of the identifyBytesFull and identifyBytes methods. The type of fileBytes being Uint16Array | Buffer required the following configuration in webpack.
// webpack.config.js
module.exports = {
//...
resolve: {
fallback: {
fs: false
}
},
};
Buffer is a type specific to Node.js and is not suitable for use in the browser. Additionally, for files used in the browser, the type Uint8Array is more commonly used instead of Uint16Array. I would like the type of fileBytes to be changed to Uint8Array for comfortable use in a browser environment.
Thanks for the quick check! Agreed on both points. 0.2.12 accepts Uint8Array and drops Buffer for non-Node magika.
I tried it out and it works like expected! Thanks for improving the js/ts lib!