简体   繁体   English

如何创建需要某些属性但可以容忍任何其他属性的类型?

[英]How to create a type that requires some properties but tolerates any additional properties?

I have a util function whose first parameter param must include (simplified for example) the properties param.X and param.Y , but it doesn't matter what other properties it has.我有一个 util function,它的第一个参数param必须包括(例如简化)属性param.Xparam.Y ,但它有什么其他属性并不重要。 For instance it can also have param.Z but that's not used by the util function and can be ignored.例如,它也可以有param.Z但它没有被 util function 使用,可以忽略。

How can I type the param such that it enforces this constraint without complaining something like ParamType does not include property Z ?我如何键入参数以使其强制执行此约束而不抱怨诸如ParamType does not include property Z

Intersect the object type with Record<PropertyKey, unknown> .将 object 类型与Record<PropertyKey, unknown>相交。

const fn = (obj: { x: string; y: string } & Record<PropertyKey, unknown>) => {
};

fn({x: 'x'}) // Fails
fn({x: 'x', y: 'y'}) // Passes
fn({x: 'x', y: 'y', z: 'z'}) // Passes

There is already an answer(more granular), this one is only to show just another way(syntactically)...已经有一个答案(更细化),这个只是为了展示另一种方式(语法上)......

type SomeRequired = {
  x: string,
  y: string,
  [key: PropertyKey]: unknown   //<--- type PropertyKey = string | number | symbol. A mapped type to accomodate additional non-required properties. We can constrain the 'unknown' here depending upon the context of use.
}

function applyFn(input: SomeRequired): void {

}

applyFn({}); // <-- Error, missing all required properties
applyFn({x: "1"}); // <-- Error, missing some of the required properties
applyFn({x: "1", y: "2"}); // <-- Ok, at least all required properties are there
applyFn({x: "1", y:"2", z: "3", p: { a: 1 }}); // Ok, additional properties along with the required ones

Use Partial<ParamType> .使用Partial<ParamType> This allows you to specify only a subset of the type.这允许您仅指定类型的一个子集。

https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Typescript 中,如何定义/键入具有附加属性的 function - In Typescript, how to define/type a function with additional properties 如何向递归类型添加附加属性 - How to add additional properties to a recursive type 如何从复杂对象的某些属性创建自定义类型? - How to create a custom type from some properties of a complex object? 带有附加属性的 function 的类型声明 - Type declaration for function with additional properties 如何创建原始类型和自定义类型的联合,原始类型的某些属性已更改并保持不变? - How to create union of original type and custom type with some properties of original changed and rest the same? 如何创建一个类型(命名或匿名),它是另一种类型,其某些属性设置为非可选? - How to create a type (named or anonymous) which is another type with some of its properties set to non-optional? 如何制作一个Typescript接口,该接口类型检查某些属性,但也允许任何属性? - How can I make a Typescript interface which type checks some properties, but also allows any prorperty? 如何使TS编译器知道未知变量具有某些属性(不使用“ any”类型) - How to make the TS compiler know an unknown variable has some properties (without the usage of `any` type) 如何键入带有附加功能作为属性的功能对象? - How do I type a function object with additional functions attached as properties? 如何从@types/jsonwebtoken 向 JwtPayload 类型添加其他属性 - How to add additional properties to JwtPayload type from @types/jsonwebtoken
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM