Skip to content

Commit e4f4369

Browse files
committed
fix(types): infer setup attrs
1 parent 12bf718 commit e4f4369

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

packages/runtime-core/src/componentOptions.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ export interface ComponentOptionsBase<
120120
EE extends string = string,
121121
Defaults = {},
122122
I extends ComponentInjectOptions = {},
123-
II extends string = string
123+
II extends string = string,
124+
Attrs = {}
124125
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, I, II>,
125126
ComponentInternalOptions,
126127
ComponentCustomOptions {
@@ -133,7 +134,7 @@ export interface ComponentOptionsBase<
133134
UnionToIntersection<ExtractOptionProp<Extends>>
134135
>
135136
>,
136-
ctx: SetupContext<E>
137+
ctx: SetupContext<E, Attrs>
137138
) => Promise<RawBindings> | RawBindings | RenderFunction | void
138139
name?: string
139140
template?: string | object // can be a direct DOM node
@@ -243,7 +244,8 @@ export type ComponentOptionsWithoutProps<
243244
EE,
244245
{},
245246
I,
246-
II
247+
II,
248+
Attrs
247249
> & {
248250
props?: undefined
249251
} & ThisType<
@@ -290,7 +292,8 @@ export type ComponentOptionsWithArrayProps<
290292
EE,
291293
{},
292294
I,
293-
II
295+
II,
296+
Attrs
294297
> & {
295298
props: PropNames[]
296299
} & ThisType<
@@ -338,7 +341,8 @@ export type ComponentOptionsWithObjectProps<
338341
EE,
339342
Defaults,
340343
I,
341-
II
344+
II,
345+
Attrs
342346
> & {
343347
props: PropsOptions & ThisType<void>
344348
} & ThisType<

test-dts/defineComponent.test-d.tsx

+27-1
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,33 @@ describe('define attrs', () => {
12541254
expectType<JSX.Element>(<MyComp bar={1} />)
12551255
})
12561256

1257-
test('define attrs w/ function component', () => {
1257+
test('define attrs w/ composition api', () => {
1258+
type CompAttrs = {
1259+
bar: number
1260+
baz?: string
1261+
}
1262+
const MyComp = defineComponent(
1263+
{
1264+
props: {
1265+
foo: {
1266+
type: String,
1267+
required: true
1268+
}
1269+
},
1270+
setup(props, { attrs }) {
1271+
expectType<string>(props.foo)
1272+
expectType<number>(attrs.bar)
1273+
expectType<string | undefined>(attrs.baz)
1274+
}
1275+
},
1276+
{
1277+
attrs: {} as CompAttrs
1278+
}
1279+
)
1280+
expectType<JSX.Element>(<MyComp foo="1" bar={1} />)
1281+
})
1282+
1283+
test('define attrs w/ functional component', () => {
12581284
type CompAttrs = {
12591285
bar: number
12601286
baz?: string

0 commit comments

Comments
 (0)