@@ -235,9 +235,15 @@ Deno.test("isObjectOf<T>", async (t) => {
235235 c : isBoolean ,
236236 } ;
237237 assertEquals ( isObjectOf ( predObj ) ( { a : 0 , b : "a" , c : true } ) , true ) ;
238+ assertEquals (
239+ isObjectOf ( predObj , { strict : true } ) ( { a : 0 , b : "a" , c : true } ) ,
240+ true ,
241+ "Specify `{ strict: true }`" ,
242+ ) ;
238243 assertEquals (
239244 isObjectOf ( predObj ) ( { a : 0 , b : "a" , c : true , d : "ignored" } ) ,
240245 true ,
246+ "Object have an unknown property" ,
241247 ) ;
242248 } ) ;
243249 await t . step ( "returns false on non T object" , ( ) => {
@@ -246,8 +252,17 @@ Deno.test("isObjectOf<T>", async (t) => {
246252 b : isString ,
247253 c : isBoolean ,
248254 } ;
249- assertEquals ( isObjectOf ( predObj ) ( { a : 0 , b : "a" , c : "" } ) , false ) ;
250- assertEquals ( isObjectOf ( predObj ) ( { a : 0 , b : "a" } ) , false ) ;
255+ assertEquals ( isObjectOf ( predObj ) ( "a" ) , false , "Value is not an object" ) ;
256+ assertEquals (
257+ isObjectOf ( predObj ) ( { a : 0 , b : "a" , c : "" } ) ,
258+ false ,
259+ "Object have a different type property" ,
260+ ) ;
261+ assertEquals (
262+ isObjectOf ( predObj ) ( { a : 0 , b : "a" } ) ,
263+ false ,
264+ "Object does not have one property" ,
265+ ) ;
251266 assertEquals (
252267 isObjectOf ( predObj , { strict : true } ) ( {
253268 a : 0 ,
@@ -256,13 +271,93 @@ Deno.test("isObjectOf<T>", async (t) => {
256271 d : "invalid" ,
257272 } ) ,
258273 false ,
274+ "Specify `{ strict: true }` and object have an unknown property" ,
259275 ) ;
260276 } ) ;
261277 await testWithExamples (
262278 t ,
263279 isObjectOf ( { a : ( _ : unknown ) : _ is unknown => true } ) ,
264280 { excludeExamples : [ "record" ] } ,
265281 ) ;
282+ await t . step ( "with optional properties" , async ( t ) => {
283+ await t . step ( "returns proper type predicate" , ( ) => {
284+ const predObj = {
285+ a : isNumber ,
286+ b : isOneOf ( [ isString , isUndefined ] ) ,
287+ c : isOptionalOf ( isBoolean ) ,
288+ } ;
289+ const a : unknown = { a : 0 , b : "a" } ;
290+ if ( isObjectOf ( predObj ) ( a ) ) {
291+ type _ = AssertTrue <
292+ IsExact < typeof a , { a : number ; b : string | undefined ; c ?: boolean } >
293+ > ;
294+ }
295+ } ) ;
296+ await t . step ( "returns true on T object" , ( ) => {
297+ const predObj = {
298+ a : isNumber ,
299+ b : isOneOf ( [ isString , isUndefined ] ) ,
300+ c : isOptionalOf ( isBoolean ) ,
301+ } ;
302+ assertEquals ( isObjectOf ( predObj ) ( { a : 0 , b : "a" , c : true } ) , true ) ;
303+ assertEquals (
304+ isObjectOf ( predObj ) ( { a : 0 , b : "a" } ) ,
305+ true ,
306+ "Object does not have an optional property" ,
307+ ) ;
308+ assertEquals (
309+ isObjectOf ( predObj ) ( { a : 0 , b : "a" , c : undefined } ) ,
310+ true ,
311+ "Object has `undefined` as value of optional property" ,
312+ ) ;
313+ assertEquals (
314+ isObjectOf ( predObj , { strict : true } ) ( { a : 0 , b : "a" , c : true } ) ,
315+ true ,
316+ "Specify `{ strict: true }`" ,
317+ ) ;
318+ assertEquals (
319+ isObjectOf ( predObj , { strict : true } ) ( { a : 0 , b : "a" } ) ,
320+ true ,
321+ "Specify `{ strict: true }` and object does not have one optional property" ,
322+ ) ;
323+ } ) ;
324+ await t . step ( "returns false on non T object" , ( ) => {
325+ const predObj = {
326+ a : isNumber ,
327+ b : isOneOf ( [ isString , isUndefined ] ) ,
328+ c : isOptionalOf ( isBoolean ) ,
329+ } ;
330+ assertEquals (
331+ isObjectOf ( predObj ) ( { a : 0 , b : "a" , c : "" } ) ,
332+ false ,
333+ "Object have a different type property" ,
334+ ) ;
335+ assertEquals (
336+ isObjectOf ( predObj ) ( { a : 0 , b : "a" , c : null } ) ,
337+ false ,
338+ "Object has `null` as value of optional property" ,
339+ ) ;
340+ assertEquals (
341+ isObjectOf ( predObj , { strict : true } ) ( {
342+ a : 0 ,
343+ b : "a" ,
344+ c : true ,
345+ d : "invalid" ,
346+ } ) ,
347+ false ,
348+ "Specify `{ strict: true }` and object have an unknown property" ,
349+ ) ;
350+ assertEquals (
351+ isObjectOf ( predObj , { strict : true } ) ( {
352+ a : 0 ,
353+ b : "a" ,
354+ d : "invalid" ,
355+ } ) ,
356+ false ,
357+ "Specify `{ strict: true }` and object have the same number of properties but an unknown property exists" ,
358+ ) ;
359+ } ) ;
360+ } ) ;
266361} ) ;
267362
268363Deno . test ( "isFunction" , async ( t ) => {
0 commit comments