The type Record
is defined within the lib.es5.d.ts file as shown below:
type Record<K extends keyof any, T> = {
[P in K]: T;
};
// The type keyof any includes string, number, or symbol
Subsequently, the type RecordWithMediaTypeKey
is defined as follows:
type MediaType = "CD" | "Stream"
type RecordWithMediaTypeKey = Record<MediaType, string>
This definition is equal to:
type RecordWithMediaTypeKey = {CD: string, Stream: string}
It's important to note that the keys here are not optional, which could explain any errors encountered.
If you require optional keys, you can utilize:
Partial<Record<MediaType, string>>
The confusion might arise from the fact that:
type RecordWithStringKey = Record<string, string>
is equivalent to:
type RecordWithStringKey = {
[x: string]: string;
}
This observation highlights the use of Index signatures.
Explore and test this code further on the TS Playground