8000 feat(types): infer required props · aztalbot/vue-function-api@0565acb · GitHub
[go: up one dir, main page]

Skip to content

Commit 0565acb

Browse files
committed
feat(types): infer required props
Resolves: vuejs#106
1 parent cb82edb commit 0565acb

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/component/component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ export function createComponent<RawBindings>(
7575
): VueProxy<never, RawBindings>;
7676
// overload 2: object format with object props declaration
7777
// see `ExtractPropTypes` in ./componentProps.ts
78-
export function createComponent<Props, RawBindings = Data, PropsOptions = ComponentPropsOptions>(
78+
export function createComponent<
79+
Props,
80+
RawBindings = Data,
81+
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
82+
>(
7983
// prettier-ignore
8084
options: (
8185
// prefer the provided Props, otherwise infer it from PropsOptions

test/types/createComponent.spec.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('createComponent', () => {
5959
return () => null;
6060
},
6161
});
62-
new Vue(App).$mount();
62+
new Vue(App);
6363
expect.assertions(3);
6464
});
6565

@@ -79,7 +79,7 @@ describe('createComponent', () => {
7979
return () => null;
8080
},
8181
});
82-
new Vue(App).$mount();
82+
new Vue(App);
8383
expect.assertions(3);
8484
});
8585

@@ -91,7 +91,41 @@ describe('createComponent', () => {
9191
return () => null;
9292
},
9393
});
94-
new Vue(App).$mount();
94+
new Vue(App);
95+
expect.assertions(2);
96+
});
97+
98+
it('infer the required prop', () => {
99+
const App = createComponent({
100+
props: {
101+
foo: {
102+
type: String,
103+
required: true,
104+
},
105+
bar: {
106+
type: String,
107+
default: 'default',
108+
},
109+
zoo: {
110+
type: String,
111+
required: false,
112+
},
113+
},
114+
propsData: {
115+
foo: 'foo',
116+
},
117+
setup(props) {
118+
type PropsType = typeof props;
119+
isSubType<{ readonly foo: string; readonly bar: string; readonly zoo?: string }, PropsType>(
120+
true
121+
);
122+
isSubType<PropsType, { readonly foo: string; readonly bar: string; readonly zoo?: string }>(
123+
true
124+
);
125+
return () => null;
126+
},
127+
});
128+
new Vue(App);
95129
expect.assertions(2);
96130
});
97131
});

0 commit comments

Comments
 (0)
0