Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Functional Components not allowing inherited attributes #3340

Closed
mklein994 opened this issue Jun 29, 2023 · 8 comments
Closed

Functional Components not allowing inherited attributes #3340

mklein994 opened this issue Jun 29, 2023 · 8 comments
Labels
good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first question Further information is requested

Comments

@mklein994
Copy link

When I run vue-tsc on a functional component that has e.g. a class="" attribute on it, I get an error1:

src/App.vue:9:33 - error TS2345: Argument of type '{ stuffAndThings: number; class: string; }' is not assignable to parameter of type '{ stuffAndThings: number; }'.
  Object literal may only specify known properties, and 'class' does not exist in type '{ stuffAndThings: number; }'.

9   <FooBar :stuff-and-things="7" class="demo" />
                                  ~~~~~

I suspect something changed that makes it not account for attributes that are inherited normally, namely class, style, and onXXX event listeners (see docs). The demo has more details, but here's the gist of it:

Demo: CodeSandbox

// src/FooBar.ts

import { h } from "vue";

function FooBar(props: { stuffAndThings: number; }) {
  return h("div", `Made it: ${props.stuffAndThings}`);
}

FooBar.props = {
  stuffAndThings: {
    type: Number,
    required: true,
  },
};

export default FooBar;
<!-- src/App.vue -->

<script setup lang="ts">
import FooBar from "./FooBar.ts";
</script>

<template>
  <FooBar :stuff-and-things="7" class="foo-bar" />
</template>

<style scoped>
.foo-bar {
  color: blue;
}
</style>

Note The demo shows a few other ways of defining a functional component, but they all have roughly the same error message.


P.S.

@johnsoncodehk Thank you for all your hard work on this project! I understand that there's been a large increase in issues recently, so it's all good, don't bend over backwards on this one ☺️. I want to encourage you (and others working on this project), that it's tools like this that make Vue and its ecosystem so enjoyable to use. To me, this language server is a gold standard that other frameworks should try to emulate. Keep up the good work! 🚀

Footnotes

  1. The line numbers may be a bit off, but the message is the same.

@so1ve
Copy link
Member

so1ve commented Jun 29, 2023

Similar: #3318, #3301

@Ctepan
Copy link

Ctepan commented Jun 30, 2023

Here is an example with functional component and component with generic in <script setup>
(vite vue-ts template + FComponent from vue docs + generic component)
https://stackblitz.com/edit/vitejs-vite-hddkhg?file=src%2FFComponent.ts

@rchl
Copy link
Collaborator

rchl commented Jul 2, 2023

Demo: CodeSandbox

Can't be opened

@sherif414
Copy link

sherif414 commented Jul 8, 2023

i normally make my functional components using arrow functions so i can anotate them with FunctionalComponent interface imported from vue

import type { FunctionalComponent } from 'vue'

interface Props {
//
}

const MyComponent: FunctionalComponent<Props, Record<string, string>> = (props) => {
    // 
}

the second generic argument passed to FunctionalComponent Record<string, string> is what stops the type errors for me
and allows passing attributes like class, style or anything else

@johnsoncodehk
Copy link
Member

Hi! first of all thank you for your encouragement, you really send me energy.

This can be solved by defining a functional component via defineComponent(), which add AllowedComponentProps, VNodeProps to props type.

import { h, defineComponent } from "vue";

const FooBar = defineComponent(
  (props: { stuffAndThings: number; }) => {
    return h("div", `Made it: ${props.stuffAndThings}`);
  }
)

@johnsoncodehk johnsoncodehk added question Further information is requested good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first labels Jul 18, 2023
@daydaystar
Copy link

Expect to solve

@so1ve
Copy link
Member

so1ve commented Oct 7, 2023

johnsoncodehk added a commit that referenced this issue May 13, 2024
@johnsoncodehk
Copy link
Member

Closed due to unable to reproduce in latest version.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants