marielle
marielle

Reputation: 438

How to type an object whose keys are a subset of the keys of the another type

I have a type like this:

type Metadata = {
  name: string;
  size: number;
  date: string;
  language: string;
  formattedName: string;
  normalizedDate: string
};

and now I need to build this constant:

const main = {
  name: ["main", "master"],
  size: ["size"],
  date: ["date"],
  language: ["lang", "language"]
} as ??? // as Record<keyof Metadata, string[]>;

that is an object whose keys are a subset of the keys of the type Metadata and the values are an array of string. How can I type the main object? I tried Partials(Metadata) but it seems to be not right. Which is the right way to do that?

Upvotes: 0

Views: 24

Answers (1)

tenshi
tenshi

Reputation: 26404

You can say that this type must "satisfy" or be at least assignable to another type, without actually overriding the inferred type information with the new satisfies keyword in 4.9+:

const main = {
  name: ["main", "master"],
  size: ["size"],
  date: ["date"],
  language: ["lang", "language"]
} satisfies Partial<Record<keyof Metadata, string[]>>;

Upvotes: 3

Related Questions