[英]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.这是一个 object,其键是元数据类型键的子集,值是字符串数组。 How can I type the
main
object?我怎样才能输入
main
的object? I tried Partials(Metadata)
but it seems to be not right.我试过
Partials(Metadata)
但它似乎不对。 Which is the right way to do that?哪个是正确的方法?
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+:您可以说此类型必须“满足”或至少可分配给另一种类型,而无需在 4.9+ 中使用新的
satisfies
关键字实际覆盖推断的类型信息:
const main = {
name: ["main", "master"],
size: ["size"],
date: ["date"],
language: ["lang", "language"]
} satisfies Partial<Record<keyof Metadata, string[]>>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.