Skip to content

Commit 8521f9d

Browse files
committed
fix(types): fix missing error for accessing undefined instance properties
fix #12718
1 parent 7161176 commit 8521f9d

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

types/test/v3/define-component-test.tsx

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,62 @@ defineComponent({
11661166
}
11671167
})
11681168

1169-
// #12742 allow attaching custom properties (consistent with v3)
1170-
const Foo = defineComponent({})
1171-
Foo.foobar = 123
1169+
describe('constructor attach custom properties', () => {
1170+
// #12742 allow attaching custom properties (consistent with v3)
1171+
const Foo = defineComponent({})
1172+
Foo.foobar = 123
1173+
})
1174+
1175+
describe('constructor instance type', () => {
1176+
const Comp = defineComponent({
1177+
data() {
1178+
return {
1179+
a: 1
1180+
}
1181+
},
1182+
1183+
computed: {
1184+
ac() {
1185+
return 1
1186+
}
1187+
},
1188+
1189+
methods: {
1190+
callA(b: number) {
1191+
return b
1192+
}
1193+
},
1194+
1195+
setup() {
1196+
return {
1197+
sa: '1'
1198+
}
1199+
}
1200+
})
1201+
1202+
const comp = new Comp()
1203+
1204+
expectType<number>(comp.a)
1205+
expectType<number>(comp.ac)
1206+
expectType<string>(comp.sa)
1207+
expectType<(b: number) => number>(comp.callA)
1208+
})
1209+
1210+
describe('should report non-existent properties in instance', () => {
1211+
const Foo = defineComponent({})
1212+
const instance = new Foo()
1213+
// @ts-expect-error
1214+
instance.foo
1215+
1216+
const Foo2 = defineComponent({
1217+
data() {
1218+
return {}
1219+
},
1220+
methods: {
1221+
example() {}
1222+
}
1223+
})
1224+
const instance2 = new Foo2()
1225+
// @ts-expect-error
1226+
instance2.foo
1227+
})

types/v3-define-component.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export type DefineComponent<
7272
*/
7373
export function defineComponent<
7474
RawBindings,
75-
D = Data,
75+
D = {},
7676
C extends ComputedOptions = {},
7777
M extends MethodOptions = {},
7878
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
@@ -101,8 +101,8 @@ export function defineComponent<
101101
*/
102102
export function defineComponent<
103103
PropNames extends string,
104-
RawBindings = Data,
105-
D = Data,
104+
RawBindings = {},
105+
D = {},
106106
C extends ComputedOptions = {},
107107
M extends MethodOptions = {},
108108
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
@@ -140,8 +140,8 @@ export function defineComponent<
140140
*/
141141
export function defineComponent<
142142
Props,
143-
RawBindings = Data,
144-
D = Data,
143+
RawBindings = {},
144+
D = {},
145145
C extends ComputedOptions = {},
146146
M extends MethodOptions = {},
147147
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy