Subscribe on changes!

Vue's mjs type error

avatar
May 11th 2023

Vue version

3.3.1

Link to minimal reproduction

https://github.com/xiaoxiangmoe/issue-vue-3-mjs-type-error

Steps to reproduce

yarn install
yarn tsc # it throw no type error
node src/vue3-default-import.mjs # it throw runtime error

What is expected?

It SHOULD throw type error

What is actually happening?

it throw NO type error

System Info

No response

Any additional comments?

in vue's package.json. it write

  "exports": {
    ".": {
      "types": "./dist/vue.d.ts",
      "import": {
        "node": "./index.mjs",
        "default": "./dist/vue.runtime.esm-bundler.js"
      },
      "require": "./index.js"
    }

Both esm and cjs is provided in same file ./dist/vue.d.ts.

Maybe we should provide seperate .d.cts and .d.mts

For example, in my repo, if run

cat node_modules/vue/dist/vue.d.ts | sed "s?../jsx?./jsx?g" > node_modules/vue/index.d.ts
cat node_modules/vue/dist/vue.d.ts | sed "s?../jsx?./jsx?g" > node_modules/vue/index.d.mts
# copy /dist/vue.d.ts to index.d.ts and index.d.mts

cp node_modules/vue/package.json node_modules/vue/package.json.bak
sed '/"types": ".\/dist\/vue.d.ts"/d' node_modules/vue/package.json.bak > node_modules/vue/package.json
# change remove types field

Then run tsc. it will throw type error.

src/vue3-default-import.mjs:2:8 - error TS1192: Module '"/Users/me/issue-vue-3-mjs-type-error/node_modules/vue/index"' has no default export.

2 import vueDefaultImport from 'vue'
         ~~~~~~~~~~~~~~~~


Found 1 error in src/vue3-default-import.mjs:2

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

This is work as intended.

avatar
May 11th 2023

Shouldn't this be considered a TypeScript bug? Vue is providing the correct types, it shouldn't matter what format the importing file is.

avatar
May 11th 2023

https://github.com/arethetypeswrong/arethetypeswrong.github.io#:~:text=Types%20are%20CJS%2C%20but%20implementation%20is%20ESM It falls into this category: "Types are CJS, but implementation is ESM". Not a big deal because Vue 3 never supported default export, but still an improvement if we can error on the type level.

avatar
May 26th 2023

closed via d621d4c6