Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the error "Generic type 'Record' requires 2 type argument(s). ts(2314)" using this form of typing?

Tags:

typescript

I'm trying to use this type but I can't:

type ROLES = "one" | "two"

type Users = {
    name: Record<[key in ROLES]?, User[]>;
};

because it throws with:

Generic type 'Record' requires 2 type argument(s). ts(2314)

Why?

like image 527
Fred Hors Avatar asked Nov 01 '25 12:11

Fred Hors


1 Answers

The error message says it all. you have to define two generics, so if you want the key to be a key of ROLES then you need to remove the ? and use Record<ROLES, User[]>. if you want optional entries you can use Partial<Record<ROLES, User[]>>.

like image 165
Dean Avatar answered Nov 04 '25 03:11

Dean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!