Skip to content
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

Typescript cannot resolve Vue Components (imported from a library) in Astro files #649

Open
1 task
leifmarcus opened this issue Sep 14, 2023 · 7 comments
Open
1 task
Labels
- P4: important Violate documented behavior or significantly improves performance (priority) feat: vue Related to the Vue integration (scope)

Comments

@leifmarcus
Copy link

Astro Info

Astro                    v3.0.13
Node                     v18.16.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             pcom-astro-vite-config
                         @astrojs/partytown
                         @astrojs/vue

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When importing a Vue component from a package/library, that is build width the vite library mode and has types declared with the vite-plugin-dts, then astro files cannot resolve the types for the props of the Vue component.

Instead, a type error is shown:

Type '{ testProp: string; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'testProp' does not exist on type 'IntrinsicAttributes'

The type declaration for this file looks like this (automatically generated during lib build):

import type { PropType } from 'vue';
declare const _default: __VLS_WithTemplateSlots<import("vue").DefineComponent<{
    testProp: {
        type: PropType<'test'>;
        default: string;
    };
}, {}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
    testProp: {
        type: PropType<'test'>;
        default: string;
    };
}>>, {
    testProp: 'test';
}, {}>, {
    default?: (_: {}) => any;
}>;
export default _default;
type __VLS_WithTemplateSlots<T, S> = T & {
    new (): {
        $slots: S;
    };
};

A temporary fix for this is to whitelist all IntrinsicAttributes in astroHTML.JSX, which will not break the typescript check, but is not really type safe.

declare namespace astroHTML.JSX {
  interface IntrinsicAttributes {
    // [key: string]: any;
  }
}

What's the expected result?

Astro files should be able to correctly interpret the typescript typings for imported Vue files with correct types for the Vue component properties.

This should also work for Vue Components, that are not imported from Vue SFC Files, but also when imported from build javascript files.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-dggmu9

Participation

  • I am willing to submit a pull request for this issue.
@Princesseuh
Copy link
Member

The ecosystem is kinda not well setup for this... The types it generates are all super Vue and Vue tooling specific. The same issue happens with Svelte. I'm not sure what's the best way to approach this at the moment.

@Princesseuh Princesseuh transferred this issue from withastro/astro Sep 14, 2023
@Princesseuh Princesseuh added bug feat: vue Related to the Vue integration (scope) labels Sep 14, 2023
@leifmarcus
Copy link
Author

Hello @Princesseuh, the vue typings are really hard to reason about, you are right. Thank you for looking into this issue.

@ryanjwilke
Copy link

ryanjwilke commented Oct 26, 2023

I believe I was having issues with this too recently where my TypeScript checks were failing on a Tag component. It was complaining that I was passing a string into the size property, yet that's was expected behavior. The code roughly looked like this:

<Tag size="24">{ label }</Tag>

As a workaround, I just removed the component and used static HTML since it was a rather simple component, but it would be nice to have this fixed as I felt like I had to introduce some tech debt in order to get around this issue.

@Princesseuh Princesseuh added - P4: important Violate documented behavior or significantly improves performance (priority) and removed bug labels Nov 12, 2023
@Psycarlo
Copy link

Having the same problem

@leifmarcus
Copy link
Author

@Princesseuh, I was thinking, the type problems might be because of a wrong typescript setup somehow? I think it would be great to have an example setup for typescript that works in the documentation. I remember, that in lower versions of Astro we needed to setup some typescript to make it work. I could imagine, that this setup might be the problem.
But I also couldn’t find how typescript needs to be set up when using Astro with vue so that it can work with Vue and Astro files.
Maybe someone has some insights about the ideal Typescript setup?

@Princesseuh
Copy link
Member

@Princesseuh, I was thinking, the type problems might be because of a wrong typescript setup somehow? I think it would be great to have an example setup for typescript that works in the documentation. I remember, that in lower versions of Astro we needed to setup some typescript to make it work. I could imagine, that this setup might be the problem.
But I also couldn’t find how typescript needs to be set up when using Astro with vue so that it can work with Vue and Astro files.
Maybe someone has some insights about the ideal Typescript setup?

The problem in the OP is just that we don't (and honestly kinda cannot) really use the .d.ts generated by Vue, as the code they generate is specific to their tooling.

No config changes can fix this, and there're no config changes required to use Astro and Vue together, as far as I'm aware

@leifmarcus
Copy link
Author

Thanks @Princesseuh. I agree. I think with the latest versions of all extensions and packages it works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P4: important Violate documented behavior or significantly improves performance (priority) feat: vue Related to the Vue integration (scope)
Projects
None yet
Development

No branches or pull requests

4 participants