question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typings for $refs should also include Component | Component[]

See original GitHub issue

Version

2.6.10

Reproduction link

https://codesandbox.io/s/p7krpry9vm

Steps to reproduce

  1. Assign a component a ref attribute, say, <my-component ref="myComponent" />
  2. Import the typings for component from vue, i.e. import { Component as VueComponent } from 'vue'; (the aliasing is needed because I am also using vue-property-decorator’s Component module, but that is not relevant to the question)
  3. Declare the types for $refs when composing a component, i.e. public readonly $refs!: { myComponent: VueComponent }

What is expected?

I expect the Component type to be accepted in the $refs definition.

What is actually happening?

I get a message that the type of this.$refs.myComponent is incompatible with the pre-existing type declared for this.$refs.<key>.

Here is a screenshot of the actual error I am getting in production code:

Code_2019-04-10_09-16-25


This issue came across because I need to access a nested element within a Vue component when writing in TypeScript. In native JS, I can simply query the DOM of the Vue component using this.$refs.myComponent.$el.querySelector(...) without an issue. However, in TypeScript, I get a warning because I need to pre-declare the typings for $refs at the top of the component, i.e.:

public readonly $refs!: {
  myComponent: VueComponent
}

When you log this.$refs.myComponent when the parent/consuming component is mounted to the console, you will see that it returns a Vue component instance. This is incompatible with the pre-existing typings for $refs, which only allows the following types: Vue | Element | Vue[] | Element[].

I propose that the typings for $refs by extended to:

{ [key: string]: Vue | Element | Component | Vue[] | Element[] | Component[] }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
HerringtonDarkholmecommented, Apr 15, 2019

I believe $refs contains either dom element(s) or component instance(s). Component in Vue’s typing is actually the component configuration object or component class, not the component instance itself.

You typing should be $refs: { myComponent: Vue} IMHO

1reaction
underfincommented, Apr 10, 2019

@terrymun
@Component({ name: 'MyComponent' }) export default class MyComponent extends Vue { } MyComponent is extends Vue, so can use $refs!: { myComponent: Vue } with generic type.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Refs with TypeScript - Medium
Refs provide a way to access DOM nodes or React elements created in the render method. Let's create some React refs with TypeScript...
Read more >
Using React Refs in Typescript - Pluralsight
This guide has shown various techniques for using refs in Typescript in different types of React components. The code for each component can...
Read more >
How to use refs in React with Typescript - Stack Overflow
If you're using React 16.3+, the suggested way to create refs is using React.createRef() . class TestApp extends React.Component<AppProps ...
Read more >
Refs and the DOM - React
Refs provide a way to access DOM nodes or React elements created in the render method. In the typical React dataflow, props are...
Read more >
How to Use React Refs with TypeScript -- newline - Fullstack.io
To use refs with functional components use a special useRef hook. It is a generic function, so we can pass a type-parameter, that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found