Here's an example of my issue (demo in ts playground)
type Tkey = 'foo' | 'bar' | 'defaultKey'
const typedObject: Record<Tkey, Boolean> = {
'foo': true,
'bar': false,
'defaultKey': true
}
// type does not persist string narrowing
const entries = Object.entries(typedObject) // [string, Boolean][]
So key
in [key, values]
is of type string
and not of type Tkey
.
As a workaround you can manually coerce after the fact, but it'd be nice to define the type during the operation.
const typedEntries = entries as [Tkey, Boolean][]