Skip to content

Commit 6f9619b

Browse files
committed
test: add readonly case
1 parent 2c16f83 commit 6f9619b

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

packages/dts-test/defineComponent.test-d.tsx

+51
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,57 @@ describe('define attrs', () => {
14021402
<Comp class={'str'} style={'str'} bar={1} foo={'str'} />
14031403
)
14041404
})
1405+
test('ignore reserved props', () => {
1406+
defineComponent({
1407+
attrs: Object as AttrsType<{
1408+
bar?: number
1409+
}>,
1410+
created() {
1411+
// @ts-expect-error reserved props
1412+
this.$attrs.key
1413+
// @ts-expect-error reserved props
1414+
this.$attrs.ref
1415+
}
1416+
})
1417+
defineComponent(
1418+
(props: { foo: string }, ctx) => {
1419+
// @ts-expect-error reserved props
1420+
ctx.attrs.key
1421+
// @ts-expect-error reserved props
1422+
ctx.attrs.ref
1423+
return () => <div>{props.foo}</div>
1424+
},
1425+
{
1426+
attrs: Object as AttrsType<{
1427+
bar?: number
1428+
}>
1429+
}
1430+
)
1431+
})
1432+
1433+
test('always readonly', () => {
1434+
defineComponent({
1435+
attrs: Object as AttrsType<{
1436+
bar?: number
1437+
}>,
1438+
created() {
1439+
// @ts-expect-error readonly
1440+
this.$attrs.class = 'test'
1441+
}
1442+
})
1443+
defineComponent(
1444+
(props: { foo: string }, ctx) => {
1445+
// @ts-expect-error readonly
1446+
ctx.attrs.bar = 1
1447+
return () => <div>{props.foo}</div>
1448+
},
1449+
{
1450+
attrs: Object as AttrsType<{
1451+
bar?: number
1452+
}>
1453+
}
1454+
)
1455+
})
14051456
})
14061457

14071458
// #5948

packages/runtime-core/src/componentOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export type StrictUnwrapAttrsType<
457457
Attrs extends Record<string, unknown>,
458458
T = NonNullable<Attrs>
459459
> = [keyof T] extends [never]
460-
? Record<string, unknown>
460+
? Readonly<Record<string, unknown>>
461461
: T extends new () => { $props: infer P }
462462
? Readonly<NonNullable<P>>
463463
: T extends (props: infer P, ...args: any) => any

packages/runtime-core/src/componentPublicInstance.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ export type ComponentPublicInstance<
218218
> = {
219219
$: ComponentInternalInstance
220220
$data: D
221-
$props: Prettify<
222-
MakeDefaultsOptional extends true
223-
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults> & AttrsProps
224-
: P & PublicProps & AttrsProps
225-
>
221+
$props: MakeDefaultsOptional extends true
222+
? Partial<Defaults> &
223+
Omit<Prettify<P> & PublicProps, keyof Defaults> &
224+
AttrsProps
225+
: Prettify<P> & PublicProps & AttrsProps
226226
$attrs: HasDefinedAttrs<Attrs> extends true
227-
? AttrsProps & AllowedComponentProps
227+
? Readonly<AttrsProps & AllowedComponentProps>
228228
: Data
229229
$refs: Data
230230
$slots: UnwrapSlotsType<S>

0 commit comments

Comments
 (0)