Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type '{}' is not assignable to type 'Record<Key, Value>'

Tags:

typescript

I have the following code:

const foo = <Key extends keyof any, Value>() {
  type Rec = Record<Key, Value>
  const a: Rec = {}
}

On the 3rd line typescript given an error that Type '{}' is not assignable to type 'Record<Key, Value>. Why is that happening?

like image 524
dps Avatar asked Oct 25 '20 16:10

dps


1 Answers

In my case generic parameter was string and it's okay to assign empty object to Record<string, unknown>.
Reason why it is not accepting {} is that Key parameter could be concrete string union type like 'prop1' | 'prop2'. In this case user is forced to add these properties to the object

like image 92
dps Avatar answered Sep 30 '22 19:09

dps