Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript misunderstanding – why are all properties required in a Record?

In this trivial example:

type AllowedKeys = 'a' | 'b'

const someObject: Record<AllowedKeys, boolean> = {a:true, b:false}

const anotherObject:  Record<AllowedKeys, boolean> = {a:true}

someObject is valid but anotherObject is not:

Property ‘b’ is missing in type ‘{ a: true; }’ but required in type ‘Record<AllowedKeys, boolean>’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Why is this?

>Solution :

Because Record basically spreads out the union into discrete properties, like this:

{
    a: boolean;
    b: boolean;
}

This is shown in the documentation:

interface CatInfo {
  age: number;
  breed: string;
}
 
type CatName = "miffy" | "boris" | "mordred";
 
const cats: Record<CatName, CatInfo> = {
  miffy: { age: 10, breed: "Persian" },
  boris: { age: 5, breed: "Maine Coon" },
  mordred: { age: 16, breed: "British Shorthair" },
};

If you want a and b to be optional, you can apply Partial:

type AllowedKeys = 'a' | 'b'

const someObject: Partial<Record<AllowedKeys, boolean>> = {a:true, b:false}
//                ^^^^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−−^
const anotherObject:  Partial<Record<AllowedKeys, boolean>> = {a:true}
//                    ^^^^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−−^

Now, both are valid.

Playground link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading