Skip to content

Type import invalid path in build #647

@zojize

Description

@zojize

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions