Type import invalid path in build
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 275
- Forks
- 35
- Avg merge
- 4h 42m
- Merged PRs (30d)
- 6
Description
Description
Types imported from parent folders have incorrect import paths and do not get recognized by TS due to the incorrect .js extension
Expected Behavior
Type import paths should end with .mts
Reproduction
https://codesandbox.io/p/devbox/l8c85t?file=%2Fdist%2Fruntime%2Futils%2Ffoo.d.ts%3A3%2C1
See the build output in dist. Type Foo imported from src/types.ts in the file src/runtime/server/utils/foo.ts using this import statement import type { Foo } from "../../../types";, got transformed to ../../../types.js which does not have a declaration file, and produces incorrect type.
Workaround
This is how I currently work around this issue.
import fs from 'node:fs'
import path from 'node:path'
export default defineBuildConfig({
hooks: {
'rollup:done': function (ctx) {
const dts = path.resolve(ctx.options.outDir, 'types.d.mts')
if (fs.existsSync(dts))
fs.cpSync(dts, path.resolve(ctx.options.outDir, 'types.d.ts'))
},
},
})
Related Issue
#308 seems relevant, but I don't know how it's fixed for them
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the build with src/runtime/server/utils/foo.ts importing Foo from src/types.ts, then inspect the generated declaration in dist. Compare the emitted ../../../types.js path with the expected .mts path and verify that Foo is recognized by TypeScript without the rollup:done workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100