Skip to content

Commit 83fe9dd

Browse files
committed
fix(types): infer setup attrs
1 parent 2c184a2 commit 83fe9dd

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,33 @@ describe('define attrs', () => {
12471247
expectType<JSX.Element>(<MyComp bar={1} />)
12481248
})
12491249

1250-
test('define attrs w/ function component', () => {
1250+
test('define attrs w/ composition api', () => {
1251+
type CompAttrs = {
1252+
bar: number
1253+
baz?: string
1254+
}
1255+
const MyComp = defineComponent(
1256+
{
1257+
props: {
1258+
foo: {
1259+
type: String,
1260+
required: true
1261+
}
1262+
},
1263+
setup(props, { attrs }) {
1264+
expectType<string>(props.foo)
1265+
expectType<number>(attrs.bar)
1266+
expectType<string | undefined>(attrs.baz)
1267+
}
1268+
},
1269+
{
1270+
attrs: {} as CompAttrs
1271+
}
1272+
)
1273+
expectType<JSX.Element>(<MyComp foo="1" bar={1} />)
1274+
})
1275+
1276+
test('define attrs w/ functional component', () => {
12511277
type CompAttrs = {
12521278
bar: number
12531279
baz?: string

packages/runtime-core/src/componentOptions.ts

Lines changed: 9 additions & 5 deletions
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<

0 commit comments

Comments
 (0)