-
-
Notifications
You must be signed in to change notification settings - Fork 370
tsx components type declaration support #669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
这样设置试试: dts: 'src/components.d.ts', |
麻烦提供个 git 仓库,我看下 |
你这个确认,其他非 tsx 组件的类型都生效了吗 |
我的意思是,除去 tsx 组件,纯 vue 的类型有生效吗,如果都没有,可以提供一个 git 仓库 |
除去tsx是生效的 |
那你提个 issue,帮忙提供个简单的 git 仓库,我看下 |
遇到了同样的问题。 但是它生成的下面这种方式的 vue 全局组件类型,在 tsx 中不能生效,没有类型提示。 declare module 'vue' {
export interface GlobalComponents {
VanButton: typeof import('vant/es')['Button']
}
} |
I reached same conclusion as OP. We need a global declaration option please. |
Gross workaround. Works great though. Use script with Save in project root import fs from 'node:fs/promises'
const template = constants => [
'export {}\ndeclare global {',
' ' + constants.join('\n '),
'}\n'
].join('\n')
const fixComponentsDts = async ({ source, target }) => {
const fileContent = await fs.readFile(source, 'utf-8')
const constants = []
const typeImportRegex = /(\w+):\s*typeof\s*import\(['"]([^'"]+)['"]\)\['(\w+)'\]/g
fileContent.replace(typeImportRegex, (_, p1, p2, p3) => {
constants.push(`const ${p1}: typeof import('${p2}')['${p3}']`)
})
await fs.writeFile(target, template(constants))
console.log(`Fixed ${source} and saved as ${target}`)
}
await fixComponentsDts({
source: './components.d.ts',
target: './components-global.d.ts'
}) Add to vite.config.js // Workaround until this pr is merged:
// https://github.com./unplugin/unplugin-vue-components/pull/673
watch({
pattern: './components.d.ts',
command: 'node ./fix-components-dts.js'
}) Add to jsconfig/tsconfig "include": [
"./src/**/*",
"./auto-imports.d.ts",
"./components-global.d.ts",
], Output export {}
declare global {
const AuthLayout: typeof import('./src/layouts/AuthLayout.jsx')['default']
...
} |
Clear and concise description of the problem
使用如下配置:
可以得到一个
components.d.ts
:然后在
App.tsx
中使用:会得到 ts 报错:


但事实上组件是可以工作的:
Suggested solution
只需要在
components.d.ts
中添加:就可以消除报错:

所以,可否在生成类型声明时额外添加全局声明以支持 tsx 组件?
虽然
unplugin-auto-import
可以做到这点,但是因为已经有了xxxResolver
感觉还是更符合知觉一点。或者如果有现成的解决方案的话请告诉我,谢谢!
Alternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: