Add explicit file extensions imports and make TypeScript checks for them #4217
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📑 Summary
When using
import ... from "file.js";
, many ESM software requires having file-extensions in the imports (for example, Node.JS has this behaviour, see https://nodejs.org/api/esm.html#mandatory-file-extensions).Currently, we don't have any tests for this, which means we've accidentally added this bug a few times before:
dayjs
imports #4170eslint
linter config, and fixes some bugs that it caught tbo47/dagre-es#12TypeScript will automatically check this for us if we use the
moduleResolution: "nodenext"
setting, see https://www.typescriptlang.org/docs/handbook/esm-node.html📏 Design Decisions
Issues
Only works with TypeScript 5.0 RC
This only works when using TypeScript v5
allowImportingTsExtensions: true
setting. This is because Vite only supports loading TypeScript files usingimport * from "file.ts";
(i.e. with.ts
extension), but TypeScript v4 only allows loading TypeScript files with the.js
extension.However, TypeScript v5 is not out yet, (only a release candidate is out). Because of that, VS Code and other editors throw TypeScript errors (unless you enable experimental settings).
We may want to avoid merging this PR until TypeScript v5 is officially out (should be out on March 14th 2023).
TypeScript doesn't check
.js
filesTypeScript currently doesn't check
.js
files, so it won't check for the correct import extensions in those files.We could add it by setting
checkJs: true
in thetsconfig.json
file, but that will probably throw up a lot of other TypeScript errors.mermaid/tsconfig.json
Line 47 in 273a9e7
Alternatively, we could also use the
import/extensions
rule fromeslint-plugin-import
only on JS files.Alternative PR
@remcohaszing has made a similar PR (see #4213) before I did, but has also tried to get it working with Cypress types. Their PR also uses TypeScript v4.9.
Since they made their PR first, I'll rather give priority to merge that PR first, if they get E2E tests working.
📋 Tasks
Make sure you
develop
branch