127

I want to type an object which can only have keys 'a', 'b' or 'c'.

So I can do it as follows:

Interface IList {
    a?: string;
    b?: string;
    c?: string;
}

They are all optional! Now I was wondering if this can be written with Record in just one line

type List = Record<'a' | 'b' | 'c', string>;

The only issue is that all keys need to be defined. So I ended up with

type List = Partial<Record<'a' | 'b' | 'c', string>>;

This works, but I can imagine there is a better way to do this without Partial. Is there an other way to make the keys optional inside Record ?

1
  • 5
    "I can imagine there is a better way" - no; I think your composition approach is the best answer (and surely the intended solution by the TypeScript designers) May 1, 2021 at 4:47

7 Answers 7

175

There is no way to specify the optionality of members of Record. They are required by definition

type Record<K extends keyof any, T> = {
    [P in K]: T; // Mapped properties are not optional, and it's not a homomorphic mapped type so it can't come from anywhere else.
};

You can define your own type if this is a common scenario for you:

type PartialRecord<K extends keyof any, T> = {
  [P in K]?: T;
};
type List =  PartialRecord<'a' | 'b' | 'c', string>

Or you can define PartialRecord using the predefined mapped types as well:

type PartialRecord<K extends keyof any, T> =  Partial<Record<K, T>>
6
  • 37
    "There is no way to specify the optionality of members of Record"... Are you certain about this? Partial<Record<keyof T, any>> seems to operate exactly this way, from what I can tell. Was this something that was changed since this answer was made?
    – KOVIKO
    Apr 30, 2019 at 19:39
  • 3
    @KOVIKO Partial is a different type, the question was if you can specify it for Record itself. The last code snippet shows exactly the ability to compose Partial with Record to get the effect of PartialRecord Apr 30, 2019 at 20:41
  • 4
    Ah, I see now that they specifically asked how to do it without Partial and I read too much into the first sentence. mb
    – KOVIKO
    Apr 30, 2019 at 21:20
  • 1
    How about Record<string, string>? It does not require specifying all possible strings. Why is there an inconsistency? Jan 24, 2021 at 1:15
  • 1
    Is it possible to specify which fields are optional and which are not for a Record?
    – Marek
    Jun 15, 2021 at 19:57
73

You can create the partial version of your List type:

type PartialList = Partial<List>;

And you could do it all on one line if you don't want the intermediate type:

type PartialList = Partial<Record<'a' | 'b' | 'c', string>>;

You might decide that, in the end, the most expressive for your future self is:

type List = {
    a?: string;
    b?: string;
    c?: string;
}
2
  • 1
    except it is likely that it is not 'a' | 'b' | 'c' and that the list is much longer and may well change over time. Would you still make that recommendation if there were 50 entries that change every few months? It is a genuine question... I have about 15 entries and am firmly on the fence at the moment! Mar 6, 2022 at 0:35
  • 1
    @AntonOfTheWoods If you have a list of 50 entries and if they could change every few months, you need to think about how you are going to use them. If they come from a database and the entries are all strings you could simply replace them with string instead of writing them out. But if you're planning to use these variable names in your code (not dynamically), you can create a list of them for type checking. Jun 18, 2022 at 9:58
30

Looks like in new versions of typescript you may do the following

type YourUnion = 'a' | 'b' | 'c';   
type ObjectWithOptionalKeys = Partial<Record<YourUnion, string>>
const someObject: ObjectWithOptionalKeys {
  a: 'str', // works
  b: 1 // throws
}
// c may not be specified at all
2
12

It's also possible to do it this way:

type List = { [P in 'a' | 'b' | 'c']?: string }; // The `?` makes the keys optional

Examples:

const validList1: List = {
  a: 'hi'
}

const validList2: List = {
  b: 'hi',
  c: 'there'
}

const validList3: List = {
  a: 'oh',
  b: 'hi',
  c: 'there'
}
0
6

Aside from the Partial<Record<List, string>> solution, there is perhaps a more obvious option to consider.

Instead, you could store your data in a map.

const map: Map<KeyType, ValueType> = new Map();

From a functional point of view there's not much difference. It really depends on the context whether this is a viable alternative or not.

2

It should be noted that all (valid) solutions to the question also allow empty Records to be passed. If you want to enforce at least a specific key you need:

interface Foo {
  a: A,
  [key: string]: B
}
2

You can also use

type Key = "a" | "b" | "c"

type List = { [key in K]?: string }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.