Skip to content

Missing type declarations #3776

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

Closed
2 tasks done
JstnMcBrd opened this issue Jun 30, 2024 · 3 comments
Closed
2 tasks done

Missing type declarations #3776

JstnMcBrd opened this issue Jun 30, 2024 · 3 comments
Labels

Comments

@JstnMcBrd
Copy link

JstnMcBrd commented Jun 30, 2024

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

When imported in a TypeScript environment, eslint-plugin-react throws a "missing type declarations" error.

image

Why

This is because eslint-plugin-react does not have any type declarations included in the package. These would usually be in an index.d.ts file.

Expected Behavior

The expected behavior is no errors. Type declarations would also be convenient for intellisense and type hints - like easily communicating to users what the available config types are, for example.

image

Workaround

Users can follow the instructions in the error message and make a temporary module augmentation to declare their own types for the package.

In my personal project, I've done it this way:

// ./src/@types/eslint-plugin-react.d.ts
declare module 'eslint-plugin-react' {
	import type { ESLint, Linter } from 'eslint';
	const plugin: Omit<ESLint.Plugin, 'configs'> & {
		// eslint-plugin-react does not use FlatConfig yet
		configs: Record<string, Linter.Config>;
	};
	export default plugin;
}

Solution

Personally, I'd always recommended writing everything in TypeScript, and then type declaration files will be automatically generated for you :)

But the quick and easy solution would be to write your own index.d.ts declaration file in the project root.

I would be happy submit a PR for this myself, if it helps.

eslint-plugin-react version

v7.34.3 (but technically all versions)

eslint version

v8.57.0 (irrelevant)

node version

v20.13.1 (irrelevant)

@JstnMcBrd JstnMcBrd added the bug label Jun 30, 2024
@ljharb
Copy link
Member

ljharb commented Jun 30, 2024

It's simply not a reasonable expectation that an npm package ships TS types, nor necessarily has a DT package. "Write everything in TS" isn't a universally palatable choice.

At some point in the future, we may ship d.ts files, but in the meantime, the proper solution (for every npm package that doesn't ship types) is to make a DT package.

@ljharb ljharb closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2024
@JstnMcBrd
Copy link
Author

Not an expectation, but certainly good to have. The effort required to write a type declaration file for a package like this is fairly low too, so I don't think it is unreasonable to suggest it. I even offered to submit a PR myself.

Hmm, a DT package does not seem preferable. The DT repo is pretty bloated, it requires devs to install yet another package, the effort required to make it is higher, and it's easier to keep types in sync when they are alongside the code. Excluding massive packages like node or react, built-in definitions are often better/easier/simpler.

You mentioned the possibility of "at some point in the future". But does closing this issue as "not planned" mean you are completely opposed to the idea? If not, I would be happy to add them now - a single index.d.ts file. It would greatly improve my devx on a personal project, so I am invested.

@ljharb
Copy link
Member

ljharb commented Jul 1, 2024

I’m not opposed to the idea, but i don’t think now is a good time to make such a PR. It’d also need to include jsdoc comments and tsc checking in CI, as well, so it’s not as simple as writing up a d.ts file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants