Skip to content

Commit

Permalink
fix(types): props type is incompatible with setup returned type
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyxu1102 committed Dec 13, 2022
1 parent fe77e2b commit 0567955
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
3 changes: 1 addition & 2 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export type DefineComponent<
PP & Props,
Defaults,
true
> &
Props
>
> &
ComponentOptionsBase<
Props,
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
extend,
isString,
isFunction,
UnionToIntersection
UnionToIntersection,
IfAny
} from '@vue/shared'
import {
toRaw,
Expand Down Expand Up @@ -167,7 +168,6 @@ export type CreateComponentPublicInstance<
ComponentOptionsBase<P, B, D, C, M, Mixin, Extends, E, string, Defaults>,
I
>

// public properties exposed on the proxy, which is used as the render context
// in templates (as `this` in the render option)
export type ComponentPublicInstance<
Expand Down Expand Up @@ -205,7 +205,7 @@ export type ComponentPublicInstance<
: (...args: any) => any,
options?: WatchOptions
): WatchStopHandle
} & P &
} & IfAny<P, P, Omit<P, keyof ShallowUnwrapRef<B>>> &
ShallowUnwrapRef<B> &
UnwrapNestedRefs<D> &
ExtractComputedReturns<C> &
Expand Down
38 changes: 31 additions & 7 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1041,13 +1041,13 @@ describe('inject', () => {
},
inject: {
foo: 'foo',
bar: 'bar',
bar: 'bar'
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
expectError(this.foobar = 1)
expectError((this.foobar = 1))
}
})

Expand All @@ -1059,7 +1059,7 @@ describe('inject', () => {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
expectError(this.foobar = 1)
expectError((this.foobar = 1))
}
})

Expand All @@ -1073,13 +1073,13 @@ describe('inject', () => {
bar: {
from: 'pbar',
default: 'bar'
},
}
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
expectError(this.foobar = 1)
expectError((this.foobar = 1))
}
})

Expand All @@ -1088,9 +1088,9 @@ describe('inject', () => {
props: ['a', 'b'],
created() {
// @ts-expect-error
expectError(this.foo = 1)
expectError((this.foo = 1))
// @ts-expect-error
expectError(this.bar = 1)
expectError((this.bar = 1))
}
})
})
Expand Down Expand Up @@ -1257,6 +1257,30 @@ describe('prop starting with `on*` is broken', () => {
})
})

describe('should work when props type is incompatible with setup returned type ', () => {
type SizeType = 'small' | 'big'
const Comp = defineComponent({
props: {
size: {
type: String as PropType<SizeType>,
required: true
}
},
setup(props) {
expectType<SizeType>(props.size)
return {
size: 1
}
}
})
type CompInstance = InstanceType<typeof Comp>

const CompA = {} as CompInstance
expectType<ComponentPublicInstance>(CompA)
expectType<number>(CompA.size)
expectType<SizeType>(CompA.$props.size)
})

// check if defineComponent can be exported
export default {
// function components
Expand Down

0 comments on commit 0567955

Please sign in to comment.