Skip to content

fix: avoid including Function type in inferred props type #301

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 7 additions & 4 deletions src/component/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ type RequiredKeys<T, MakeDefaultRequired> = {

type OptionalKeys<T, MakeDefaultRequired> = Exclude<keyof T, RequiredKeys<T, MakeDefaultRequired>>;

// We wrap every conditional type with tuple as we don't want to distribute it.
// About distributive conditional types: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types
// Related issue: https://github.com/vuejs/composition-api/issues/291
// prettier-ignore
type InferPropType<T> = T extends null
type InferPropType<T> = [T] extends [null]
? any // null & true would fail to infer
: T extends { type: null }
: [T] extends [{ type: null }]
? any // somehow `ObjectConstructor` when inferred from { (): T } becomes `any`
: T extends ObjectConstructor | { type: ObjectConstructor }
: [T] extends [ObjectConstructor | { type: ObjectConstructor }]
? { [key: string]: any }
: T extends Prop<infer V, true | false>
: [T] extends [Prop<infer V, true | false>]
? V
: T;

Expand Down
27 changes: 26 additions & 1 deletion test/types/defineComponent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { createComponent, defineComponent, createElement as h, ref, SetupContext, PropType } from '../../src';
import {
createComponent,
defineComponent,
createElement as h,
ref,
SetupContext,
PropType,
} from '../../src';
import Router from 'vue-router';

const Vue = require('vue/dist/vue.common.js');
Expand Down Expand Up @@ -105,6 +112,24 @@ describe('defineComponent', () => {
expect.assertions(3);
});

it('custom props type inferred from PropType', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is passing on master branch 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is because the behavior prior TS 3.9 because PropsType includes {} type which can be assigned to { user?: User }. 🤔

https://devblogs.microsoft.com/typescript/announcing-typescript-3-9-rc/

Generally, an intersection type like A & B is assignable to C if either A or B is assignable to C

Once I updated TS to 3.9, the test fails on master.

interface User {
name: string;
}
const App = defineComponent({
props: {
user: Object as PropType<User>,
},
setup(props) {
type PropsType = typeof props;
isSubType<{ user?: User }, PropsType>(true);
isSubType<PropsType, { user?: User }>(true);
},
});
new Vue(App);
expect.assertions(2);
});

it('no props', () => {
const App = defineComponent({
setup(props, ctx) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"downlevelIteration": true,
"noUnusedLocals": true
},
"include": ["src"]
"include": ["src", "test"]
}