Skip to content

Commit

Permalink
fix(types): contravariant generic default in ComponentOption (vuejs#7369
Browse files Browse the repository at this point in the history
)
  • Loading branch information
HerringtonDarkholme authored and hefeng committed Jan 25, 2019
1 parent 0559e21 commit 60466a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 4 additions & 3 deletions types/options.d.ts
Expand Up @@ -6,16 +6,17 @@ type Constructor = {
}

// we don't support infer props in async component
export type Component<Data=DefaultData<Vue>, Methods=DefaultMethods<Vue>, Computed=DefaultComputed, Props=DefaultProps> =
// N.B. ComponentOptions<V> is contravariant, the default generic should be bottom type
export type Component<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> =
| typeof Vue
| FunctionalComponentOptions<Props>
| ComponentOptions<Vue, Data, Methods, Computed, Props>
| ComponentOptions<never, Data, Methods, Computed, Props>

interface EsModuleComponent {
default: Component
}

export type AsyncComponent<Data=DefaultData<Vue>, Methods=DefaultMethods<Vue>, Computed=DefaultComputed, Props=DefaultProps> = (
export type AsyncComponent<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> = (
resolve: (component: Component<Data, Methods, Computed, Props>) => void,
reject: (reason?: any) => void
) => Promise<Component | EsModuleComponent> | void;
Expand Down
20 changes: 16 additions & 4 deletions types/test/options-test.ts
@@ -1,11 +1,23 @@
import Vue, { VNode } from "../index";
import { AsyncComponent, ComponentOptions, FunctionalComponentOptions } from "../index";
import { AsyncComponent, ComponentOptions, FunctionalComponentOptions, Component } from "../index";
import { CreateElement } from "../vue";

interface Component extends Vue {
interface MyComponent extends Vue {
a: number;
}

const option: ComponentOptions<MyComponent> = {
data() {
return {
a: 123
}
}
}

// contravariant generic should use never
const anotherOption: ComponentOptions<never> = option
const componentType: Component = option

Vue.component('sub-component', {
components: {
a: Vue.component(""),
Expand Down Expand Up @@ -41,10 +53,10 @@ Vue.component('string-prop', {
});

class User {
private u: number
private u = 1
}
class Cat {
private u: number
private u = 1
}

Vue.component('union-prop', {
Expand Down

0 comments on commit 60466a2

Please sign in to comment.