Skip to content

Commit

Permalink
fix(TypeScript): Force ArrayConstructor to be a any[] typescript type
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Domino9697 committed May 27, 2019
1 parent 06d4ad4 commit 791f5aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export interface RenderContext<Props=DefaultProps> {
injections: any
}

export type Prop<T> = { (): T } | { new(...args: any[]): T & object } | { new(...args: string[]): Function }
export type Prop<T> = { (): T extends {}[] ? any[] : T } | { new(...args: any[]): T & object } | { new(...args: string[]): Function }

export type PropType<T> = Prop<T> | Prop<T>[];

Expand Down
19 changes: 19 additions & 0 deletions types/test/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ Vue.component('prop-with-primitive-default', {
}
});

// Test issue with the Array prop type
Vue.extend({
props: {
s: String,
a: {type: Array},
b: Boolean
},
created() {
console.log(this.a)
console.log(this.s)
console.log(this.b)
},
mounted() {
console.log(this.a)
console.log(this.s)
console.log(this.b)
}
})

Vue.component('component', {
data() {
this.$mount
Expand Down

0 comments on commit 791f5aa

Please sign in to comment.