From 8521f9d3f63d26bde99b747f0cb14d0ac5ba5971 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 19 Aug 2022 12:22:51 +0800 Subject: [PATCH] fix(types): fix missing error for accessing undefined instance properties fix #12718 --- types/test/v3/define-component-test.tsx | 62 +++++++++++++++++++++++-- types/v3-define-component.d.ts | 10 ++-- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/types/test/v3/define-component-test.tsx b/types/test/v3/define-component-test.tsx index 481a2d11ac7..7e6d1968ca3 100644 --- a/types/test/v3/define-component-test.tsx +++ b/types/test/v3/define-component-test.tsx @@ -1166,6 +1166,62 @@ defineComponent({ } }) -// #12742 allow attaching custom properties (consistent with v3) -const Foo = defineComponent({}) -Foo.foobar = 123 +describe('constructor attach custom properties', () => { + // #12742 allow attaching custom properties (consistent with v3) + const Foo = defineComponent({}) + Foo.foobar = 123 +}) + +describe('constructor instance type', () => { + const Comp = defineComponent({ + data() { + return { + a: 1 + } + }, + + computed: { + ac() { + return 1 + } + }, + + methods: { + callA(b: number) { + return b + } + }, + + setup() { + return { + sa: '1' + } + } + }) + + const comp = new Comp() + + expectType(comp.a) + expectType(comp.ac) + expectType(comp.sa) + expectType<(b: number) => number>(comp.callA) +}) + +describe('should report non-existent properties in instance', () => { + const Foo = defineComponent({}) + const instance = new Foo() + // @ts-expect-error + instance.foo + + const Foo2 = defineComponent({ + data() { + return {} + }, + methods: { + example() {} + } + }) + const instance2 = new Foo2() + // @ts-expect-error + instance2.foo +}) diff --git a/types/v3-define-component.d.ts b/types/v3-define-component.d.ts index 69f65a2ec35..03ef52d1856 100644 --- a/types/v3-define-component.d.ts +++ b/types/v3-define-component.d.ts @@ -72,7 +72,7 @@ export type DefineComponent< */ export function defineComponent< RawBindings, - D = Data, + D = {}, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, @@ -101,8 +101,8 @@ export function defineComponent< */ export function defineComponent< PropNames extends string, - RawBindings = Data, - D = Data, + RawBindings = {}, + D = {}, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, @@ -140,8 +140,8 @@ export function defineComponent< */ export function defineComponent< Props, - RawBindings = Data, - D = Data, + RawBindings = {}, + D = {}, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,