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

add typescript type checking/emitting based on jsdoc strings #200

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

sfishel18
Copy link
Contributor

fixes #199

@sfishel18 sfishel18 force-pushed the just-enough-typescript branch from 925510e to 5883575 Compare March 16, 2024 21:27
@sfishel18 sfishel18 changed the title initial commit of tsconfig.json file and some initial jsdoc updates add typescript type checking/emitting based on jsdoc strings Mar 25, 2024
value: parseFloat(
averageIntensity.data[device.country?.toUpperCase()]
),
// TODO (simon) check that parseFloat can be safely removed here
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: the types i could find suggest that averageIntensity.data[device.country?.toUpperCase()] will always be a number, so parseFloat is unnecessary. are there cases where that could be a string?

*/
async function loadJSON(jsonPath) {
const jsonBuffer = jsonPath.endsWith(".gz")
? await getGzippedFileAsJson(jsonPath)
: await readFile(jsonPath);
return JSON.parse(jsonBuffer);
// TODO (simon) should we check that the parsed JSON is a list of strings?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: typescript itself can't guarantee that what comes out of JSON.parse is actually a list of strings. would it be worth adding some runtime checking to validate?

* @template {Record<string, number>} EnergyObject
* @param {EnergyObject} energyByComponent - energy grouped by the four system components
* // TODO (simon) check on this type for carbonIntensity
* @param {(number | boolean)=} carbonIntensity - carbon intensity to apply to the datacentre values
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: it looks like this param is being used as a boolean but is initialized to a number when not provided. i'm wondering if it should be initialized to false instead?

src/co2.js Outdated
const allAssets = [];
for (let asset of pageXray.assets) {
const domain = new URL(asset.url).domain;
const domain = new URL(asset.url).host;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: is this change correct? typescript doesn't recognize that there is a domain property of a URL instance, but it's possible it's inferring the wrong URL type

@sfishel18
Copy link
Contributor Author

@fershad / @mrchrisadams i think this is ready for a first look from you. i tried to hit the right balance between getting some maintainable types without adding too much complex typescript logic that might be a barrier to entry for contributors. but of course i'll defer to you on what that balance should be, so let me know if i should lean more toward the "fancy" typescript features or go in the other direction and make everything more explicit.

specifically the "fancy" stuff i'm referring to is in the utility types at the bottom of types.js, which i think are helpful in encapsulating some repeated patterns i found in mapping between different values, but their inner workings are probably not very approachable to a typescript beginner.

in terms of leaning toward more complex typescript logic, what i had in mind was for methods like perByte and perVisit where the return type can be different depending on the arguments to the method or to the class constructor. in theory i think we could encode that into the type system itself, which would be nice for consumers of the library since they would be able to statically infer the return type in their own code, but would involve some relatively complex typescript logic.

@sfishel18 sfishel18 force-pushed the just-enough-typescript branch from af49944 to c3b4a2e Compare April 14, 2024 01:58
@sfishel18 sfishel18 force-pushed the just-enough-typescript branch from c3b4a2e to fbbeabf Compare April 14, 2024 02:22
@sfishel18 sfishel18 force-pushed the just-enough-typescript branch from fbbeabf to 73ef44d Compare April 30, 2024 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add type-check and type-emit steps for type annotation code to the current js code
2 participants