From 6ee684983b1f3384a4050d7c47cee7c6a325db8b Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Wed, 3 Jan 2018 14:33:48 +0800 Subject: [PATCH] fix(types): contravariant generic default in ComponentOption (#7369) --- types/options.d.ts | 7 ++++--- types/test/options-test.ts | 20 ++++++++++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/types/options.d.ts b/types/options.d.ts index 8401a93b..a54993ff 100644 --- a/types/options.d.ts +++ b/types/options.d.ts @@ -6,16 +6,17 @@ type Constructor = { } // we don't support infer props in async component -export type Component, Methods=DefaultMethods, Computed=DefaultComputed, Props=DefaultProps> = +// N.B. ComponentOptions is contravariant, the default generic should be bottom type +export type Component, Methods=DefaultMethods, Computed=DefaultComputed, Props=DefaultProps> = | typeof Vue | FunctionalComponentOptions - | ComponentOptions + | ComponentOptions interface EsModuleComponent { default: Component } -export type AsyncComponent, Methods=DefaultMethods, Computed=DefaultComputed, Props=DefaultProps> = ( +export type AsyncComponent, Methods=DefaultMethods, Computed=DefaultComputed, Props=DefaultProps> = ( resolve: (component: Component) => void, reject: (reason?: any) => void ) => Promise | void; diff --git a/types/test/options-test.ts b/types/test/options-test.ts index a62d9bb2..a14560fe 100644 --- a/types/test/options-test.ts +++ b/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 = { + data() { + return { + a: 123 + } + } +} + +// contravariant generic should use never +const anotherOption: ComponentOptions = option +const componentType: Component = option + Vue.component('sub-component', { components: { a: Vue.component(""), @@ -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', {