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.

Props not recognized by Typescript when using render functions

See original GitHub issue

Defining props works great unless you’re using render functions. In that case, Typescript will detect an error TS2322. Not sure what does Typescript expect from the component type to work…

Below is an example of the error. I could provide a minimal repository if someone needs to play with it.

import { Component, Prop, Vue } from "vue-property-decorator";

@Component
export class Heading1 extends Vue {
    @Prop({ type: String, default: "" })
    public readonly title!: string;

    public render(): JSX.Element {
        return <h1>{this.title}</h1>;
    }
}

@Component
export class Page extends Vue {
    public render(): JSX.Element {
        return <Heading1 title="Test" />;
    }
}
index.tsx:16:17 - error TS2322: Type '{ title: string; }' is not assignable to type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.
  Property 'title' does not exist on type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.

16         return <Heading title="Test" />;
                   ~~~~~~~


Found 1 error.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ArmorDarkscommented, May 25, 2019

Turns out you need to explicitly register used components. You can’t use imported components directly:

won’t work:

import { Vue, Component } from 'vue-property-decorator'

import Layout from './components/Layout'

@Component
export default class App extends Vue {
  render () {
    // Will error
    return <Layout class='something'><router-view /></Layout>
  }
}

will work

import { Vue, Component } from 'vue-property-decorator'

import Layout from './components/Layout'

@Component({
  components: {
    Layout
  }
})
export default class App extends Vue {
  render () {
    return <layout class='something'><router-view /></layout>
  }
}

No attributes suggestions will be provided by VS Code anyway, though.

Registering manually each component feels like a sad step-back to old Vue-ways, but for now, seems to be the only “proper” way.

1reaction
kaorun343commented, Mar 20, 2019

@skyrpex

Hi.

When defining Vue component with vue-class-component, TS compiler cannot detect which properties are defined as props. On the other hand, when defining with Vue.extend, it can.

There’s no solution that vue-property-decorator can provide. Could ask this issue at vue or vue-class-component repository instead? Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React/typescript doesn't recognize component when passed ...
I have a generic functional component where the props of this components consists of additional components that should be rendered.
Read more >
React Render Props in TypeScript. Update - Medium
This article will use the function-as-a-child render prop pattern to match its usage within React itself for the new context API.
Read more >
Render a React Component with State and Props using ...
Though both Functional and Class Components can contain props and a state, this guide will cover how to create and render a Class...
Read more >
How to Use TypeScript with React Components
B) Then use the interface to annotate the props parameter inside the functional component function. For example, let's annotate a component ...
Read more >
Documentation - JSX - TypeScript
Additionally the output will have a .jsx file extension. The react mode will emit React.createElement , does not need to go through a...
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