Skip to content

chore: Fix type import specifier linting#175

Merged
rekmarks merged 3 commits intomainfrom
rekm/type-import-linting
Oct 21, 2024
Merged

chore: Fix type import specifier linting#175
rekmarks merged 3 commits intomainfrom
rekm/type-import-linting

Conversation

@rekmarks
Copy link
Copy Markdown
Member

This PR reconfigures our tsconfig files and ESLint config to enforce consistent type import specifiers and better type elision behavior.

Heretofore, we have enforced the existence of type import specifiers, but that they are stylistically consistent. The options are "top-level" (import type { a } from 'x'}) and "inline" (import { type a } from 'x'}). This was enforced using the ESLint rule @typescript-eslint/consistent-type-imports. This rule has an option named fixTypes, but that only applies when a type-only import is not specified as a type-only import. In other words, it enforces the existence of type import specifiers, but not that they're stylistically consistent.

After consulting the rule documentation, the TypeScript docs, and typescript-eslint/typescript-eslint#9652, I determined that we are better off enabling verbatimModuleSyntax in our tsconfig files, and replacing @typescript-eslint/consistent-type-imports with import-x/consistent-type-specifier-style. In this way, the existence of type import specifiers is enforced by tsc, and the import-x ESLint plugins ensures that they are consistent. We use the "top-level" style because that results in import type { a } from 'x'; being elided completely from the JS output as opposed to import { type a } from 'x'; which would be emitted as import {} from 'x';. I don't know if that's actually harmful, but it seems surprising, and I don't like surprises.

@rekmarks rekmarks requested a review from a team as a code owner October 21, 2024 22:37
Copy link
Copy Markdown
Contributor

@sirtimid sirtimid left a comment

Choose a reason for hiding this comment

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

Nice

@rekmarks rekmarks enabled auto-merge (squash) October 21, 2024 23:53
@rekmarks rekmarks merged commit e978dfb into main Oct 21, 2024
@rekmarks rekmarks deleted the rekm/type-import-linting branch October 21, 2024 23:55
rekmarks added a commit to MetaMask/eslint-config that referenced this pull request Feb 28, 2025
Ref: MetaMask/ocap-kernel#175

This replaces `@typescript-eslint/consistent-type-imports` with `import-x/consistent-type-specifier-style`, and an exhortation to enable `verbatimModuleSyntax` (requires TypeScript >=5) in the tsconfigs of our packages.

Heretofore, we have enforced the existence of type import specifiers, but not that they are stylistically consistent. The options are:
- "top-level", e.g. `import type { a } from 'x'}`
- "inline", e.g. `import { type a } from 'x'}`
 
The rule `@typescript-eslint/consistent-type-imports` has been responsible for this. This rule has an option named `fixTypes`, but that only applies when a type-only import is _not_ specified as a type-only import. In other words, it enforces the existence of type import specifiers, but not that they're stylistically consistent.

Rather than using `@typescript-eslint/consistent-type-imports` and its confusingly named options, we are better off enabling `verbatimModuleSyntax` in our `tsconfig` files, and enabling `import-x/consistent-type-specifier-style`. In this way, the existence of type import specifiers is enforced by `tsc`, and the `import-x` ESLint plugins ensures that they are consistent. We use the "top-level" style because that results in `import type { a } from 'x';` being elided completely from the JS output as opposed to `import { type a } from 'x';` which would be emitted as `import {} from 'x';`. I don't know if that's actually harmful, but it seems surprising, and I don't like surprises.

Supporting documentation:
- `@typescript-eslint/consistent-type-imports` [rule documentation](https://typescript-eslint.io/rules/consistent-type-imports/#comparison-with-importsnotusedasvalues--verbatimmodulesyntax)
- [The `verbatimModuleSyntax` docs](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax)
- This tseslint issue: typescript-eslint/typescript-eslint#9652
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.

2 participants