简体   繁体   English

如何在打字稿中为函数定义对象接受的参数类型

[英]How to define object accepted argument type for function in typescript

Lets say I want to define a function that accepts one argument that must be an object which keys are strings and values can be for example number | string假设我想定义一个函数,该函数接受一个参数,该参数必须是一个对象,其中键是字符串,值可以是例如number | string number | string like it done in the bottom example. number | string在底部示例中所做的那样。

const fn = (param: Record<string, string | number>) => {
  console.log(param);
};

const p = {
  id: 1,
  name: 'John'
};

fn(p);

the above example will work just fine, but when I want to give an explicit type to variable p with using typescript's type as in the bottom example, everything working just fine again.上面的示例可以正常工作,但是当我想使用 typescript 的type为变量p提供显式类型时,如底部示例中所示,一切正常。

const fn = (param: Record<string, string | number>) => {
  console.log(param);
};

type User = {
  id: number;
  name: string;
}

const p: User = {
  id: 1,
  name: 'John'
};

fn(p);

But when defining type with the typescript interface as in the example below I'm getting an error saying但是当使用 typescript interface定义类型时,如下例所示,我收到一条错误消息
TS2345: Argument of type 'User' is not assignable to parameter of type 'Record<string, string | number>' TS2345: Argument of type 'User' is not assignable to parameter of type 'Record<string, string | number>' . TS2345: Argument of type 'User' is not assignable to parameter of type 'Record<string, string | number>'
Index signature for type 'string' is missing in type 'User'.

const fn = (param: Record<string, string | number>) => {
  console.log(param);
};

interface User {
  id: number;
  name: string;
}

const p: User = {
  id: 1,
  name: 'John'
};

fn(p);

So what is the reason for such a behavior?那么这种行为的原因是什么? How can I define type for function argument to be an object which keys should be string and values can be union type for example string | number如何将函数参数的类型定义为一个对象,其中键应该是string ,值可以是联合类型,例如string | number string | number and will work with both type s and interface s? string | number并且可以同时使用type s 和interface s?

REVISED:修改:

You can destructure the object p as such when calling the function:您可以在调用函数时解构对象p

fn({ ...p });

This takes all of the properties of p and puts them on a new object.这会获取p的所有属性并将它们放在一个新对象上。

It is also possible to create a new object for the purposes of passing to the function:也可以创建一个新对象来传递给函数:

const params = {
    ...p,
};

fn(params);

This should be due to the fact that interfaces are "open" while type aliases are closed, meaning they can not be extended after their initial declaration, unlike interfaces, that can be declared again - which means you could add properties to the interface, that are not assignable to Record<string, string | number>这应该是因为接口是“开放的”,而类型别名是关闭的,这意味着它们在初始声明后无法扩展,与可以再次声明的接口不同 - 这意味着您可以向接口添加属性,即不可分配给Record<string, string | number> Record<string, string | number> . Record<string, string | number> . This also relates to declaration merging这也与声明合并有关

See this playground for more details有关更多详细信息,请参阅此游乐场

暂无
暂无

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

相关问题 如何根据Typescript中的字符串参数定义函数的参数类型? - How to define a function's argument type dependent on string argument in Typescript? 如何在TypeScript的嵌套对象中键入具有函数参数类型的函数参数? - How to type a function argument with a function argument type in a nested object in TypeScript? 如何定义将类型作为强类型参数的Typescript函数 - How to define Typescript function that takes a type as a strongly typed argument React.Dispatch 时如何为 setState function 定义 TypeScript 类型<react.setstateaction<string> &gt; 不接受? </react.setstateaction<string> - How can I define TypeScript type for a setState function when React.Dispatch<React.SetStateAction<string>> not accepted? 打字稿:为箭头函数的对象参数定义类型 - Typescript: Define type for object param of arrow function [Typescript]:如何键入通用 function 以接受更具体的 object 作为参数? - [Typescript]: How to type a generic function to accept a more specific object as argument? 正确输入 function object 参数 TypeScript - Properly type function object argument TypeScript TypeScript 在 function 中输入参数 - TypeScript type argument in function if args like object TypeScript:通过推断函数的参数类型来定义类型 - TypeScript: Define a type by inferring a Function's Argument Types TypeScript:你能根据函数的参数定义一个返回类型结构吗? - TypeScript: Can you define a return Type structure based on an argument of the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM