vite-plugin-dts icon indicating copy to clipboard operation
vite-plugin-dts copied to clipboard

vue2 dts 生成,default 关键词

Open rorry121 opened this issue 2 years ago • 10 comments

版本:1.3.1


import type { ShallowUnwrapRef, Ref, Data, ExtractPropTypes, ComponentRenderProxy } from '@vue/composition-api';
import type { ComponentOptions, default, VueConstructor } from 'vue';
import { PropType } from '@vue/composition-api';
declare const _sfc_main: ComponentOptions<default, ShallowUnwrapRef<{
    name: Ref<string>;
}> & Data, {}, {}, {
    value: {
        type: PropType<string>;
        default: string;
    };
}, ExtractPropTypes<{
    value: {
        type: PropType<string>;
        default: string;
    };
}>> & Omit<VueConstructor<default>, never> & (new (...args: any[]) => ComponentRenderProxy<{
    value: string;
} & {}, ShallowUnwrapRef<{
    name: Ref<string>;
}>, Data, {}, {}, {}, {}, {}, {
    value: string;
} & {}, {
    value: string;
}, true>);
export default _sfc_main;

这是打包生成的 d.ts 文件,其中 第二行导出 default 编辑器会标红, 在项目中引用也不能准确类型,看起来是语法错误?

如果改成

import type { ComponentOptions, default as DT, VueConstructor } from 'vue';

也就是给 default 用一个别名 DT,把下面的用到的 default 换成 DT,就 ok 了。 这里是因为 default 是一个关键字? 有什么解决方案吗?

rorry121 avatar Jul 21 '22 15:07 rorry121

image 修改后: image

rorry121 avatar Jul 21 '22 15:07 rorry121

你的源代码是怎么样的?

qmhc avatar Jul 22 '22 00:07 qmhc

@qmhc https://github.com/rorry121/vite-vue2-dts-test

rorry121 avatar Jul 22 '22 06:07 rorry121

你提供的这个仓库我没法正常的运行打包。

你是否有尝试升级使用 [email protected]+ 的版本。

qmhc avatar Jul 25 '22 06:07 qmhc

sry,版本问题,请重新拉一下代码

rorry121 avatar Jul 25 '22 07:07 rorry121

我用 npm 无法安装成功,你是否可以尝试切换到 pnpm,或者你使用 vue2.7 来替换 @vue/composition-api

qmhc avatar Jul 25 '22 07:07 qmhc

已更新,可以试试 pnpm ,

目前还发现 1.3.1 版本 index.es.d.ts 生成也有些问题,1.3.0 没问题

我用 npm 无法安装成功,你是否可以尝试切换到 pnpm,或者你使用 vue2.7 来替换 @vue/composition-api

rorry121 avatar Jul 25 '22 12:07 rorry121

        compilerOptions: {
            baseUrl: ".",
            paths: {
                "vue": ["node_modules/vue"],
                "@vue/composition-api": ["node_modules/@vue/composition-api"]
            }
        }

改成

        compilerOptions: {
            baseUrl: ".",
            paths: {
                "vue": ["node_modules/vue"],
                "vue/*": ["node_modules/vue"],
                "@vue/composition-api": ["node_modules/@vue/composition-api"]
            }
        }

就好了

我之前有尝试过

        compilerOptions: {
            baseUrl: ".",
            paths: {
                "vue": ["node_modules/vue"],
                "vue/*": ["node_modules/vue/*"],
                "@vue/composition-api": ["node_modules/@vue/composition-api"]
            }
        }

没有效果

rorry121 avatar Jul 28 '22 02:07 rorry121

我也遇到了类似的问题:

<template>
    <button>ok</button>
</template>
<script lang="ts" setup>
import { Setup } from 'vue-class-setup';

export interface Xss {
    size?: 'extra-small' | 'small' | 'large' | 'extra-large';
}

@Setup
class X {}
const state = new X();
</script>

上面这段代码是正常的,但是size加上 default的值时,编译会报错

lzxb avatar Aug 11 '22 12:08 lzxb

下面这个会报错,仅仅只是多了default,哪怕把代码注释掉。

size?: 'extra-small' | 'small' | 'large' | 'extra-large' | 'default';

lzxb avatar Aug 11 '22 12:08 lzxb

3.0.0 开始使用 Volar 作为驱动,对 Vue 版本的支持将跟随 Volar,请使用 Vue2.7及以上的版本。

qmhc avatar Jun 30 '23 03:06 qmhc