Skip to content

types(runtime-core): improve the extracted instance types #1936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ export function defineComponent<
Extends,
E,
VNodeProps & AllowedComponentProps & ComponentCustomProps
>
> &
Readonly<ExtractPropTypes<PropsOptions>>
> &
ComponentOptionsWithObjectProps<
PropsOptions,
Expand Down
57 changes: 56 additions & 1 deletion test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('with object props', () => {
// default + function
ffff: {
type: Function as PropType<(a: number, b: string) => { a: boolean }>,
default: (a: number, b: string) => ({ a: true })
default: (a: number, b: string) => ({ a: a > +b })
},
validated: {
type: String,
Expand Down Expand Up @@ -799,3 +799,58 @@ describe('componentOptions setup should be `SetupContext`', () => {
ctx: SetupContext
) => any)
})

describe('extract instance type', () => {
const Base = defineComponent({
props: {
baseA: {
type: Number,
default: 1
}
}
})
const MixinA = defineComponent({
props: {
mA: {
type: String,
default: ''
}
}
})
const CompA = defineComponent({
extends: Base,
mixins: [MixinA],
props: {
a: {
type: Boolean,
default: false
},
b: {
type: String,
required: true
},
c: Number
}
})

const compA = {} as InstanceType<typeof CompA>

expectType<boolean>(compA.a)
expectType<string>(compA.b)
expectType<number | undefined>(compA.c)
// mixins
expectType<string>(compA.mA)
// extends
expectType<number>(compA.baseA)

// @ts-expect-error
expectError((compA.a = true))
// @ts-expect-error
expectError((compA.b = 'foo'))
// @ts-expect-error
expectError((compA.c = 1))
// @ts-expect-error
expectError((compA.mA = 'foo'))
// @ts-expect-error
expectError((compA.baseA = 1))
})