[英]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.