Skip to content

Property 'xxx' does not exist on type 'ComponentPublicInstance' #2775

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

Closed
terminalqo opened this issue Dec 9, 2020 · 13 comments
Closed

Property 'xxx' does not exist on type 'ComponentPublicInstance' #2775

terminalqo opened this issue Dec 9, 2020 · 13 comments

Comments

@terminalqo
Copy link

terminalqo commented Dec 9, 2020

Version

"vue": "^3.0.0"
"eslint": "^6.7.2"
"typescript": "~3.9.3"
vue-cli@latest

Reproduction link

Steps to reproduce

import { defineComponent, PropType } from "vue";

interface ComplexMessage {
  title: string;
  okMessage: string;
  cancelMessage: string;
}

const BarCharts = defineComponent({
  setup() {},
  props: {
    message: {
      type: Object as PropType<ComplexMessage>,
      required: true,
      validator(message: ComplexMessage) {
        return !!message.title;
      }
    }
  },
  data() {
    return {};
  },
  created() {
    console.log(this.message); // ts2339
  },
  render() {
    return <div>1</div>;
  }
});

export default BarCharts;

What is expected?

No ts warning

What is actually happening?

ts warning show:

Property 'message' does not exist on type 'ComponentPublicInstance<Readonly<{ [x: number]: string; } & { length?: number | undefined; toString?: string | undefined; toLocaleString?: string | undefined; concat?: string[] | undefined; join?: string | undefined; ... 17 more ...; flat?: unknown[] | undefined; }> | Readonly<...>, ... 8 more ..., ComponentOptionsB...'.
  Property 'message' does not exist on type '{ $: ComponentInternalInstance; $data: {}; $props: Readonly<{ [x: number]: string; } & { length?: number | undefined; toString?: string | undefined; toLocaleString?: string | undefined; ... 19 more ...; flat?: unknown[] | undefined; }> | Readonly<...>; ... 10 more ...; $watch(source: string | Function, cb: Function,...'.ts(2339)

file name: BarCharts.tsx,
use vscode vetur@latest.

@terminalqo
Copy link
Author

image

@terminalqo
Copy link
Author

terminalqo commented Dec 9, 2020

@Zcating
Copy link
Contributor

Zcating commented Dec 9, 2020

You should use arrow function while using validator or default attr. #2474

@littlesmilelove
Copy link

You should use arrow function while using validator or default attr. #2474

This answer is correct.

@ravenclaw900
Copy link

ravenclaw900 commented Dec 21, 2020

I'm having the same problem, but with data instead of props. I don't have any validators or anything like that, and it works just fine when using javascript.

<script lang="ts">
export default {
  data() {
    return {
      platform: "x86_64"
    };
  },
  computed: {
    showMachines() {
      return this.platform === "arm" || this.platform === "aarch64"; // Error here, on platform
    },
    showAarch64() {
      return this.platform === "aarch64"; // Here too, same place
    }
  }
};
</script>

@LinusBorg
Copy link
Member

  1. use defineComponent() to let TS know you are actually defining a Vue component
  2. try annotating the return values of the computed's:
showMachines(): boolean {

For more help, please use chat.vuejs.org

@bobohuochai
Copy link

I'm having the same problem, but with data instead of props. I don't have any validators or anything like that, and it works just fine when using javascript.

<script lang="ts">
export default {
  data() {
    return {
      platform: "x86_64"
    };
  },
  computed: {
    showMachines() {
      return this.platform === "arm" || this.platform === "aarch64"; // Error here, on platform
    },
    showAarch64() {
      return this.platform === "aarch64"; // Here too, same place
    }
  }
};
</script>

The same problem with you. It's confused.

@LinusBorg
Copy link
Member

@LinusBorg LinusBorg reopened this Mar 1, 2021
@nandodrw
Copy link

@ravenclaw900 @bobohuochai as @LinusBorg point as soon you define the return types of your computed properties the types from the data properties will work just fine(looks like not doing it breaks all the typing on the component).

@Liangzhihe
Copy link

  1. use defineComponent() to let TS know you are actually defining a Vue component
  2. try annotating the return values of the computed's:
showMachines(): boolean {

For more help, please use chat.vuejs.org

thank you very much!

@sakurawald
Copy link

  1. use defineComponent() to let TS know you are actually defining a Vue component
  2. try annotating the return values of the computed's:
showMachines(): boolean {

For more help, please use chat.vuejs.org

This works. The problem of TypeScript

@benjsmyth
Copy link

benjsmyth commented May 3, 2023

I had this problem for objects that I was injecting into my component. For anyone having problems with typing injected data, the solution is to use the Composition API instead.

@popring
Copy link

popring commented Aug 10, 2023

  1. use defineComponent() to let TS know you are actually defining a Vue component
  2. try annotating the return values of the computed's:
showMachines(): boolean {

For more help, please use chat.vuejs.org

thanks, also correct resolve my problem, ts error with data.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests