thegreenwebfoundation / co2.js

An npm module for accessing the green web API, and estimating the carbon emissions from using digital services
Other
385 stars 48 forks source link

Add a module declaration for use from typescript #131

Closed hamishfagg closed 1 year ago

hamishfagg commented 1 year ago

Adding this module declaration means that importing co2 from typescript is possible with:

import { co2 } from '@tgwf/co2'

fershad commented 1 year ago

Just help me understand this a bit better @hamishfagg, as I don't have a lot of TS experience. Bit of a broader chat about converting CO2.js to TS in #77.

I'm under the impression that TS handles JS natively, but I understand that having this would help folks using CO2.js in TS projects with compilation and type-checking in their IDE. Is that right?

It would also mean that this file needs to be included when the library is packaged for NPM, right?

hamishfagg commented 1 year ago

I have very little TS experience as well so maybe someone can chime in who knows.

But basically without this file TS complains that it doesn't know what the co2 module is. With it, I'm able to import and use co2 normally.

It would also mean that this file needs to be included when the library is packaged for NPM, right?

Correct

mishushakov commented 1 year ago

Currently, TypeScript users get an error or a warning, that co2.js lacks typescript type definitions. You don't have to convert the library to TypeScript to fix this, you can just make a .d.ts file and define the types there. Otherwise, I'd recommend upgrading the library to TypeScript, simply out of fact, that the majority people in WebDev are using TS and expect everything to work with TS

fershad commented 1 year ago

@mishushakov thanks for the extra context. I've merged this in, as a first step towards making the library more TS friendly. It's just enough TS.

We'd be open to a PR add more types for the hosting & data modules, or add other .d.ts files. I might be up for a TS refactor (and to learn TS myself) later in the year, but for now we have a few higher priority features we'd like to introduce.