For example, I have the below type and I'm using an es6 object to serve as a dictionary.
type User = {
id: string,
name: string
}
const usersDict = { [key: string]: User } = {}
But the problem is if I try to get a nonexistent user id, the above typing isn't correct. e.g.
const user = usersDict['user_id_that_doesnt_exist'] // type of user is User
Should I type usersDict as below instead?
const usersDict = { [key: string]: User | undefined } = {}
The problem for this one would be when looping through the values in the below example
const usersDict = { [key: string]: User | undefined } = {}
Object.values(usersDict).map((user)=>{
// here the type of user is User|undefined,
// but it really should be just User type,
// since it's impossible to be undefined
})