Skip to content

Commit 5008238

Browse files
authored
Add new dateProp prop type (#774)
1 parent 6ca7ac2 commit 5008238

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,21 @@ integerProp().withDefault(42);
263263
// → prop type: number
264264
```
265265

266+
### `dateProp(validator?: Validator)`
267+
268+
Allows any `Date` object (validated at runtime and compile time).
269+
270+
```ts
271+
dateProp().optional;
272+
// → prop type: Date | undefined
273+
dateProp().nullable;
274+
// → prop type: Date | null
275+
dateProp().required;
276+
// → prop type: Date
277+
dateProp().withDefault(() => new Date());
278+
// → prop type: Date
279+
```
280+
266281
### `symbolProp(validator?: Validator)`
267282

268283
Allows any symbol (validated at runtime and compile time).

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { stringProp } from './prop-types/string';
22
export { booleanProp } from './prop-types/boolean';
33
export { numberProp } from './prop-types/number';
44
export { integerProp } from './prop-types/integer';
5+
export { dateProp } from './prop-types/date';
56
export { symbolProp } from './prop-types/symbol';
67
export { vueComponentProp } from './prop-types/vueComponent';
78

src/prop-types/date.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { PropOptionsGenerator, PropType } from '../types';
2+
import { propOptionsGenerator } from '../util';
3+
import type { Validator } from '../validators';
4+
5+
/**
6+
* Allows any `Date` object (validated at runtime and compile time).
7+
*
8+
* @param validator - Optional function for further runtime validation; should return `undefined` if valid, or an error string if invalid.
9+
*/
10+
export const dateProp = (validator?: Validator): PropOptionsGenerator<Date> =>
11+
propOptionsGenerator(Date as unknown as PropType<Date>, validator);

tests/prop-types/date.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { dateProp } from '../../src/prop-types/date';
3+
4+
describe('dateProp().optional', () => {
5+
it('creates the correct prop options', () => {
6+
expect(dateProp().optional).toStrictEqual({
7+
type: Date,
8+
required: false,
9+
default: undefined,
10+
validator: undefined,
11+
});
12+
});
13+
});
14+
15+
describe('dateProp().nullable', () => {
16+
it('creates the correct prop options', () => {
17+
expect(dateProp().nullable).toStrictEqual({
18+
type: Date,
19+
required: false,
20+
default: null,
21+
validator: undefined,
22+
});
23+
});
24+
});
25+
26+
describe('dateProp().withDefault', () => {
27+
it('creates the correct prop options', () => {
28+
expect(dateProp().withDefault(() => new Date())).toStrictEqual({
29+
type: Date,
30+
required: false,
31+
default: expect.any(Function),
32+
validator: undefined,
33+
});
34+
});
35+
});
36+
37+
describe('dateProp().required', () => {
38+
it('creates the correct prop options', () => {
39+
expect(dateProp().required).toStrictEqual({
40+
type: Date,
41+
required: true,
42+
validator: undefined,
43+
});
44+
});
45+
});
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { describe, expect, test } from 'tstyche';
2+
import type * as Vue2_6 from 'vue2-6/types/options';
3+
import type * as Vue2_7 from 'vue2-7/types/options';
4+
import type * as Vue3 from '@vue/runtime-core/dist/runtime-core';
5+
import { dateProp } from '../../src/prop-types/date';
6+
import { createVue2Component } from '../utils';
7+
import type { Vue2ComponentWithProp } from '../utils';
8+
9+
describe('dateProp().optional', () => {
10+
test('Vue 2.6', () => {
11+
expect<Vue2_6.PropOptions<Date | undefined>>().type.toBeAssignableWith(
12+
dateProp().optional,
13+
);
14+
expect<Vue2_6.PropOptions<Date>>().type.not.toBeAssignableWith(
15+
dateProp().optional,
16+
);
17+
18+
expect(createVue2Component(dateProp().optional)).type.toBe<
19+
Vue2ComponentWithProp<Date | undefined>
20+
>();
21+
});
22+
23+
test('Vue 2.7', () => {
24+
expect<Vue2_7.PropOptions<Date | undefined>>().type.toBeAssignableWith(
25+
dateProp().optional,
26+
);
27+
expect<Vue2_7.PropOptions<Date>>().type.not.toBeAssignableWith(
28+
dateProp().optional,
29+
);
30+
});
31+
32+
test('Vue 3', () => {
33+
expect<Vue3.Prop<Date | undefined>>().type.toBeAssignableWith(
34+
dateProp().optional,
35+
);
36+
expect<Vue3.Prop<Date>>().type.not.toBeAssignableWith(dateProp().optional);
37+
});
38+
});
39+
40+
describe('dateProp().nullable', () => {
41+
test('Vue 2.6', () => {
42+
expect<Vue2_6.PropOptions<Date | null>>().type.toBeAssignableWith(
43+
dateProp().nullable,
44+
);
45+
expect<Vue2_6.PropOptions<Date>>().type.not.toBeAssignableWith(
46+
dateProp().nullable,
47+
);
48+
49+
expect(createVue2Component(dateProp().nullable)).type.toBe<
50+
Vue2ComponentWithProp<Date | null>
51+
>();
52+
});
53+
54+
test('Vue 2.7', () => {
55+
expect<Vue2_7.PropOptions<Date | null>>().type.toBeAssignableWith(
56+
dateProp().nullable,
57+
);
58+
expect<Vue2_7.PropOptions<Date>>().type.not.toBeAssignableWith(
59+
dateProp().nullable,
60+
);
61+
});
62+
63+
test('Vue 3', () => {
64+
expect<Vue3.Prop<Date | null>>().type.toBeAssignableWith(
65+
dateProp().nullable,
66+
);
67+
expect<Vue3.Prop<Date>>().type.not.toBeAssignableWith(dateProp().nullable);
68+
});
69+
});
70+
71+
describe('dateProp().withDefault(false)', () => {
72+
test('Vue 2.6', () => {
73+
expect<Vue2_6.PropOptions<Date>>().type.toBeAssignableWith(
74+
dateProp().withDefault(() => new Date()),
75+
);
76+
expect<Vue2_6.PropOptions<string>>().type.not.toBeAssignableWith(
77+
dateProp().withDefault(() => new Date()),
78+
);
79+
80+
expect(
81+
createVue2Component(dateProp().withDefault(() => new Date())),
82+
).type.toBe<Vue2ComponentWithProp<Date>>();
83+
});
84+
85+
test('Vue 2.7', () => {
86+
expect<Vue2_7.PropOptions<Date>>().type.toBeAssignableWith(
87+
dateProp().withDefault(() => new Date()),
88+
);
89+
expect<Vue2_7.PropOptions<string>>().type.not.toBeAssignableWith(
90+
dateProp().withDefault(() => new Date()),
91+
);
92+
});
93+
94+
test('Vue 3', () => {
95+
expect<Vue3.Prop<Date>>().type.toBeAssignableWith(
96+
dateProp().withDefault(() => new Date()),
97+
);
98+
expect<Vue3.Prop<string>>().type.not.toBeAssignableWith(
99+
dateProp().withDefault(() => new Date()),
100+
);
101+
});
102+
});
103+
104+
describe('dateProp().required', () => {
105+
test('Vue 2.6', () => {
106+
expect<Vue2_6.PropOptions<Date>>().type.toBeAssignableWith(
107+
dateProp().required,
108+
);
109+
expect<Vue2_6.PropOptions<string>>().type.not.toBeAssignableWith(
110+
dateProp().required,
111+
);
112+
113+
expect(createVue2Component(dateProp().required)).type.toBe<
114+
Vue2ComponentWithProp<Date>
115+
>();
116+
});
117+
118+
test('Vue 2.7', () => {
119+
expect<Vue2_7.PropOptions<Date>>().type.toBeAssignableWith(
120+
dateProp().required,
121+
);
122+
expect<Vue2_7.PropOptions<string>>().type.not.toBeAssignableWith(
123+
dateProp().required,
124+
);
125+
});
126+
127+
test('Vue 3', () => {
128+
expect<Vue3.Prop<Date>>().type.toBeAssignableWith(dateProp().required);
129+
expect<Vue3.Prop<string>>().type.not.toBeAssignableWith(
130+
dateProp().required,
131+
);
132+
});
133+
});

0 commit comments

Comments
 (0)