@@ -14,6 +14,7 @@ describe('with object props', () => {
14
14
b : string
15
15
e ?: Function
16
16
bb : string
17
+ bbb : string
17
18
cc ?: string [ ] | undefined
18
19
dd : { n : 1 }
19
20
ee ?: ( ) => string
@@ -23,6 +24,9 @@ describe('with object props', () => {
23
24
eee : ( ) => { a : string }
24
25
fff : ( a : number , b : string ) => { a : boolean }
25
26
hhh : boolean
27
+ ggg : 'foo' | 'bar'
28
+ ffff : ( a : number , b : string ) => { a : boolean }
29
+ validated ?: string
26
30
}
27
31
28
32
type GT = string & { __brand : unknown }
@@ -40,6 +44,11 @@ describe('with object props', () => {
40
44
bb : {
41
45
default : 'hello' ,
42
46
} ,
47
+ bbb : {
48
+ // Note: default function value requires arrow syntax + explicit
49
+ // annotation
50
+ default : ( props : any ) => ( props . bb as string ) || 'foo' ,
51
+ } ,
43
52
// explicit type casting
44
53
cc : Array as PropType < string [ ] > ,
45
54
// required + type casting
@@ -68,10 +77,25 @@ describe('with object props', () => {
68
77
type : Function as PropType < ( a : number , b : string ) => { a : boolean } > ,
69
78
required : true ,
70
79
} ,
80
+ // default + type casting
81
+ ggg : {
82
+ type : String as PropType < 'foo' | 'bar' > ,
83
+ default : 'foo' ,
84
+ } ,
71
85
hhh : {
72
86
type : Boolean ,
73
87
required : true ,
74
88
} ,
89
+ // default + function
90
+ ffff : {
91
+ type : Function as PropType < ( a : number , b : string ) => { a : boolean } > ,
92
+ default : ( _a : number , _b : string ) => ( { a : true } ) ,
93
+ } ,
94
+ validated : {
95
+ type : String ,
96
+ // validator requires explicit annotation
97
+ validator : ( val : unknown ) => val !== '' ,
98
+ } ,
75
99
} ,
76
100
setup ( props ) {
77
101
// type assertion. See https://github.com/SamVerschueren/tsd
@@ -83,11 +107,15 @@ describe('with object props', () => {
83
107
expectType < ExpectedProps [ 'dd' ] > ( props . dd )
84
108
expectType < ExpectedProps [ 'ee' ] > ( props . ee )
85
109
expectType < ExpectedProps [ 'ff' ] > ( props . ff )
110
+ expectType < ExpectedProps [ 'bbb' ] > ( props . bbb )
86
111
expectType < ExpectedProps [ 'ccc' ] > ( props . ccc )
87
112
expectType < ExpectedProps [ 'ddd' ] > ( props . ddd )
88
113
expectType < ExpectedProps [ 'eee' ] > ( props . eee )
89
114
expectType < ExpectedProps [ 'fff' ] > ( props . fff )
115
+ expectType < ExpectedProps [ 'ggg' ] > ( props . ggg )
90
116
expectType < ExpectedProps [ 'hhh' ] > ( props . hhh )
117
+ expectType < ExpectedProps [ 'ffff' ] > ( props . ffff )
118
+ expectType < ExpectedProps [ 'validated' ] > ( props . validated )
91
119
92
120
isNotAnyOrUndefined ( props . a )
93
121
isNotAnyOrUndefined ( props . b )
@@ -97,11 +125,14 @@ describe('with object props', () => {
97
125
isNotAnyOrUndefined ( props . dd )
98
126
isNotAnyOrUndefined ( props . ee )
99
127
isNotAnyOrUndefined ( props . ff )
128
+ isNotAnyOrUndefined ( props . bbb )
100
129
isNotAnyOrUndefined ( props . ccc )
101
130
isNotAnyOrUndefined ( props . ddd )
102
131
isNotAnyOrUndefined ( props . eee )
103
132
isNotAnyOrUndefined ( props . fff )
133
+ isNotAnyOrUndefined ( props . ggg )
104
134
isNotAnyOrUndefined ( props . hhh )
135
+ isNotAnyOrUndefined ( props . ffff )
105
136
106
137
expectError ( ( props . a = 1 ) )
107
138
@@ -126,11 +157,15 @@ describe('with object props', () => {
126
157
expectType < ExpectedProps [ 'dd' ] > ( props . dd )
127
158
expectType < ExpectedProps [ 'ee' ] > ( props . ee )
128
159
expectType < ExpectedProps [ 'ff' ] > ( props . ff )
160
+ expectType < ExpectedProps [ 'bbb' ] > ( props . bbb )
129
161
expectType < ExpectedProps [ 'ccc' ] > ( props . ccc )
130
162
expectType < ExpectedProps [ 'ddd' ] > ( props . ddd )
131
163
expectType < ExpectedProps [ 'eee' ] > ( props . eee )
132
164
expectType < ExpectedProps [ 'fff' ] > ( props . fff )
165
+ expectType < ExpectedProps [ 'ggg' ] > ( props . ggg )
133
166
expectType < ExpectedProps [ 'hhh' ] > ( props . hhh )
167
+ expectType < ExpectedProps [ 'ffff' ] > ( props . ffff )
168
+ expectType < ExpectedProps [ 'validated' ] > ( props . validated )
134
169
135
170
// @ts -expect-error props should be readonly
136
171
expectError ( ( props . a = 1 ) )
@@ -144,10 +179,13 @@ describe('with object props', () => {
144
179
expectType < ExpectedProps [ 'dd' ] > ( this . dd )
145
180
expectType < ExpectedProps [ 'ee' ] > ( this . ee )
146
181
expectType < ExpectedProps [ 'ff' ] > ( this . ff )
182
+ expectType < ExpectedProps [ 'bbb' ] > ( this . bbb )
147
183
expectType < ExpectedProps [ 'ccc' ] > ( this . ccc )
148
184
expectType < ExpectedProps [ 'ddd' ] > ( this . ddd )
149
185
expectType < ExpectedProps [ 'eee' ] > ( this . eee )
150
186
expectType < ExpectedProps [ 'fff' ] > ( this . fff )
187
+ expectType < ExpectedProps [ 'ggg' ] > ( this . ggg )
188
+ expectType < ExpectedProps [ 'ffff' ] > ( this . ffff )
151
189
expectType < ExpectedProps [ 'hhh' ] > ( this . hhh )
152
190
153
191
// @ts -expect-error props on `this` should be readonly
0 commit comments