ts 中使用 import导入包 包的.d.ts文件貌似没有生效

我在vue3的app.vue里导入我写的包

<script lang="ts">
import { defineComponent } from 'vue'

import obj from '@xinbear/test2'

console.log('obj', obj)
obj.test2() // 出现问题的语句

// console.log(a)

export default defineComponent({
  name: 'App'
})
</script>

如果我不执行obj.test2()程序是可以运行的
但我加上obj.test2()便会报错

TS2339: Property 'test2' does not exist on type 'typeof import("/Users/xinzhongjie/work/me_project/lerna-vue3/packages/test2/lib/test2")'.

image.png

这是我vue3项目中node_modules下的包
image.png
test2.js

'use strict';
const obj = {
    test2: function(val) {
        console.log('val:', val)
    }
}
module.exports = obj;

test2.d.ts

export declare type obj = {
    test2: (val: string) => void
}

package.json

{
  "name": "@xinbear/test2",
  "version": "1.0.0",
  "description": "> TODO: description",
  "author": "xinbear <xxxxxxxx@qq.com>",
  "homepage": "",
  "license": "ISC",
  "main": "lib/test2.js",
  "directories": {
    "lib": "lib",
    "test": "__tests__"
  },
  "files": [
    "lib"
  ],
  "publishConfig": {
    "registry": "https://registry.npm.taobao.org/"
  },
  "types": "lib/test2.d.ts",
  "scripts": {
    "test": "echo \"Error: run tests from root\" && exit 1"
  }
}
阅读 6.3k
2 个回答
declare namespace obj{
    test2: (val: string) => void
}

你那个 obj 也不是默认导出呀。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题