I create an function to select and keyof of deep object. but I don't know why this keyof on my object is not work when it is an option.
type Database = {
User?: {
id: string
name: string
},
Post?: { // if i remove ? it work but it should be an option
id: string
content: string
}
}
type SelectQuery<T> = {
[Table in keyof T]?: {
select: {
[field in keyof T[Table]]?: boolean
}
}
}
function select(query: SelectQuery<Database>) {}
select({
Post: {
select: {
// it should suggest content and id here
}
}
})
The problem is that you have nested optional fields in your
Databasetype and when you access one table you get a typetableFields | undefinedandkeyofreturnsneverfor this case. Example:To fix this issue you can use built-in NonNullable utility type:
Usage:
playground