Skip to content

tsx components type declaration support #669

Open
@pawover

Description

@pawover

Clear and concise description of the problem

使用如下配置:

import { VantResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';

export const configComponents = () => {
  return Components({
    extensions: ['js', 'jsx', 'ts', 'tsx', 'vue'],
    dts: true,
    resolvers: [VantResolver()],
    include: [/\.vue$/, /\.vue\?vue/, /\.md$/, /\.tsx$/, /\.jsx$/],
  });
};

可以得到一个 components.d.ts:

/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// Generated by unplugin-vue-components
// Read more: https://github.com/vuejs/core/pull/3399
export {}

declare module 'vue' {
  export interface GlobalComponents {
    RouterLink: typeof import('vue-router')['RouterLink']
    RouterView: typeof import('vue-router')['RouterView']
    VanButton: typeof import('vant/es')['Button']
  }
}

然后在 App.tsx 中使用:

export const App = defineComponent(() => {
  return () => (
    <>
      <VanButton type="primary">myButton</VanButton>
      <RouterView />
    </>
  );
});

会得到 ts 报错:
image
但事实上组件是可以工作的:
image

Suggested solution

只需要在 components.d.ts 中添加:

declare global {
   const RouterLink: typeof import('vue-router')['RouterLink']
   const RouterView: typeof import('vue-router')['RouterView']
   const VanButton: typeof import('vant/es')['Button']
}

就可以消除报错:
image

所以,可否在生成类型声明时额外添加全局声明以支持 tsx 组件?
虽然 unplugin-auto-import 可以做到这点,但是因为已经有了 xxxResolver 感觉还是更符合知觉一点。

或者如果有现成的解决方案的话请告诉我,谢谢!

Alternative

No response

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions