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

feat: Add type checking and type definitions #266

Merged
merged 3 commits into from
Aug 5, 2024
Merged

feat: Add type checking and type definitions #266

merged 3 commits into from
Aug 5, 2024

Conversation

nzakas
Copy link
Member

@nzakas nzakas commented Jul 24, 2024

This adds TypeScript into the project:

  • Added type definitions to validate the code
  • Generated type definitions for the package
  • Added Rollup to create a single file to distribute

@mdjermanovic
Copy link
Member

There are merge conflicts here now.

@nzakas
Copy link
Member Author

nzakas commented Jul 26, 2024

Thanks, fixed.

Copy link

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

The source code looks great + straightforward to me! Nice to see the existing ad-hoc types generally matching up.

Mainly requesting changes on two things:

  • Updating the ESLint config to reflect that this is formal TypeScript-in-JS now
  • Using a build system more tailored to compiling TS types

{ src: "src/types.ts", dest: "dist/esm" }
]
})
]

Choose a reason for hiding this comment

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

Is using Rollup + plugins a settled choice? I've had an easier time with tsup as a more TypeScript-native builder.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's what we've been using in all of the type-checked repos so far. Given that it's working for us, I don't think this PR is the place to ponder alternatives. However, it is a bit clunky, so an exploration of another option would be welcome as a separate PR.

"allowJs": true,
"checkJs": true,
"outDir": "dist/esm",
"target": "ES2022",

Choose a reason for hiding this comment

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

Strict mode is a strong recommendation for TypeScript projects:

Suggested change
"target": "ES2022",
"strict": true,
"target": "ES2022",

If the codebase/team isn't ready for it just yet, there should be a TODO for adding it in.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is an open question. It might be recommended, but I've found it to be overly cumbersome and I'm not sold that the value is worth the pain. We can follow up on that question later.

/**
* @fileoverview Strips typedef aliases from the rolled-up file. This
* is necessary because the TypeScript compiler throws an error when
* it encounters a duplicate typedef.

Choose a reason for hiding this comment

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

Do you have a reference here? My instinct is that one of the following must be true:

  • This is a case of the repo's tooling "holding it wrong": which would be a good thing to fix (e.g. by switching from Rollup to something like tsup that's more commonly used with TS builds)
  • This is an actual bug in TypeScript that we can link to & I'd want to know more about
  • This is a really common thing we'd want to have standardized tooling for (which would surprise me, but you never know!)

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought this was intended behavior? Basically, if you have two JSDoc typedefs with the same name, tsc errors out. You can test this for yourself by checking out this branch and removing the dedupe-types step from npm run build and then running it.

Copy link

@JoshuaKGoldberg JoshuaKGoldberg Jul 30, 2024

Choose a reason for hiding this comment

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

It might be intended behavior on the TypeScript side to error on this case, but it's not intended behavior for the case to be present in the first place. In other words: it's not intended that everybody writing types-in-JS should have to write tools to work around quirks of type checking and transpiling in this way. This kind of bespoke tooling shouldn't be normal or required for any kind of TypeScript projects.

Copy link
Member Author

Choose a reason for hiding this comment

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

Gotcha. I guess I assumed it was akin to defining the same variable twice with let or const.

src/processor.js Show resolved Hide resolved

Choose a reason for hiding this comment

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

(commenting here because eslint.config.js isn't changed) Now that files are being generally validated & type checked with TypeScript, the ESLint config should switch from jsdoc.configs["flat/recommended"] to jsdoc.configs["flat/recommended-typescript-flavor"] (or some similar addition of TypeScript). https://github.com/gajus/eslint-plugin-jsdoc/blob/2fbd47c3d2b23a41d87b979daa56a648270d3675/README.md#eslintrc

Copy link
Member Author

Choose a reason for hiding this comment

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

That change would need to be made in the main repo, which contains eslint-config-eslint.

@nzakas
Copy link
Member Author

nzakas commented Aug 5, 2024

Merging, as this is blocking my progress on the Markdown language feature.

@nzakas nzakas merged commit 0503748 into main Aug 5, 2024
11 checks passed
@nzakas nzakas deleted the types branch August 5, 2024 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants