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

Typing: Props not bound to this #9935

Closed
octref opened this issue Apr 26, 2019 · 9 comments
Closed

Typing: Props not bound to this #9935

octref opened this issue Apr 26, 2019 · 9 comments

Comments

@octref
Copy link
Member

octref commented Apr 26, 2019

Version

Vue: 2.6.10
TS: 3.3.4000 and 3.4.5

Reproduction link

https://github.com/octref/vue-prop-type-error

Steps to reproduce

  • git clone https://github.com/octref/vue-prop-type-error && cd vue-prop-type-error
  • yarn
  • yarn compile

What is expected?

No error reported from this file

import Vue from 'vue'

const foo = Vue.extend({
  props: {
    a: Array,
    s: String,
    b: Boolean
  },
  created() {
    console.log(this.a)
    console.log(this.s)
    console.log(this.b)
  }
})

What is actually happening?

a, s, b are not bound to this instance

index.ts:10:22 - error TS2339: Property 'a' does not exist on type 'ComponentOptions<Vue, DefaultDat
a<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, a
ny>>'.

10     console.log(this.a)
                        ~

index.ts:11:22 - error TS2339: Property 's' does not exist on type 'ComponentOptions<Vue, DefaultDat
a<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, a
ny>>'.

11     console.log(this.s)
                        ~

index.ts:12:22 - error TS2339: Property 'b' does not exist on type 'ComponentOptions<Vue, DefaultDat
a<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.

12     console.log(this.b)
                        ~


Found 3 errors.
@jackkoppa
Copy link

Perhaps a duplicate of #9873

Both root causes seem to be from this TypeScript issue: microsoft/TypeScript#30854

@dodas
Copy link

dodas commented May 4, 2019

It is clear how to fix this in case of computed properties, but what should we do in this case?

Domino9697 added a commit to Domino9697/vue that referenced this issue May 27, 2019
When using an Array prop in a Vue component with TypeScript, typescript breaks because the
JavaScript arrayconstructor is transpiled to a {}[] typescript type that is for some reason more
general than any[]. Since the type is more general, the component is not using the correct component
overload function declaration. The idea here is to force the JavaScript ArrayConstructor to be
transpiled to any[] type.

fix vuejs#9935
@p-kuen
Copy link

p-kuen commented May 28, 2019

I had this issue a couple of times. This is mostly caused by the Array prop.
If you cast only the array prop, everything works fine, although it's very strange.

No errors using the following script:

import Vue from 'vue'

const foo = Vue.extend({
  props: {
    a: Array as () => any[],
    s: String,
    b: Boolean
  },
  created() {
    console.log(this.a)
    console.log(this.s)
    console.log(this.b)
  }
})

I think this is only working with TS <= 3.3.4000

@Domino9697
Copy link

@Patcher56 Indeed ! The issue comes from the Array prop. The problem is that in the VueJS type declaration files, all the JavaScript types (eg String, Number, Array) need to be translated to TypeScript types.

Now for some reason in the case of the Array prop, TypeScript infers it as a type that is "as general" as the any type. The wrong TypeScript Vue.extend overload is thus used (it uses the generic one without any props) leading all the props of your component not to be inferred by TypeScript.

My PR solves this issue by forcing the type coming from an Array prop to be of type any[].

@sem4phor
Copy link

For me the error occurs on not Array props too! See microsoft/TypeScript#30854 (comment)

@Domino9697
Copy link

By the way, this issue does not appear if you test it with TypeScript 3.5 (in development). Instead of having an array typed as any[], it will be typed as unknown[], thus solving this issue.

@ACVis
Copy link

ACVis commented Jul 24, 2019

This issue still occurs for me, with Arrays, while using TypeScript 3.5.3 and Vue 2.6.10. @Patcher56 workaround was the only thing that fixed it. Although, incredibly, it took me hours of googling to find this solution. Hopefully @Domino9697 's PR gets merged.

@sem4phor
Copy link

sem4phor commented Jul 25, 2019

This might be related:
microsoft/TypeScript#30854

@ktsn
Copy link
Member

ktsn commented Sep 10, 2019

This is already fixed on TypeScript >= 3.5.

@ktsn ktsn closed this as completed Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants