grantila/suretype

How to define object key & values?

crobinson42 opened this issue · 3 comments

I can't seem to find a solution for typing object's key=>value types, ie:

type Item = {
 [key: string]: StoreItem
}

Something like this would be a nice addition to the API:

import { v } from 'suretype'

const Item = v.objectKeyValue( v.string(), v.object({ ... }) )

@crobinson42 try an empty object (i.e. with no specific property defined) and use additional. The key being a string is implied.

v.object( )
    .additional( v.object( { ... } ) );

Please re-open if this didn't do it!

@grantila please re-open this issue - this error happens with your suggestion: TypeError: Cannot convert undefined or null to object

Disregard - it works with an empty object:

v.object( {} )
    .additional( v.object( { ... } ) );