QuizNot attempted
Generic constraints
1–4 to choose, Enter to submit
Show answer & explanation
Correct answer:
keycould be any string; use<T, K extends keyof T>(obj: T, key: K): T[K]
key: stringcould be any string, soobj[key]is not known to exist onT- constraining
K extends keyof Tmakes the access safe and gives a preciseT[K]return type. - Not 'use any': generics can index objects once constrained, and
anydiscards safety. - Not 'declare as object':
objecthas no known keys either. - Not obj.key: that reads a literal property named
key.