typescript - 打字稿,定义一个通用的代替对象
问题描述
目前我有这个接口:
export interface requestPayload {
agentId: string;
}
export interface Agent {
id: string;
name: string;
requestPayload: requestPayload;
}
我的问题是接口的requestPayload
属性Agent
现在是一个对象{ agentId: string; }
但requestPayload
应该是任何东西,比如数组、数字、字符串,也许还有我用requestPayload
接口定义的那个对象。
我收到了“使用泛型”的建议,如何将其更改为泛型?我不确定如何进行。
解决方案
对于您的情况,使用泛型是非常正确的解决方案:
export interface Agent<T> {
id: string;
name: string;
requestPayload: T;
}
推荐阅读
- forms - Xamarin Forms with Prism - 使用 WebService 的问题
- java - JPA 仅返回 @ManyToOne 对象的第一个实例
- python - keras:更改图像对象的尺寸以输入 ANN?
- c++ - std::minstd_rand 的界限是什么?
- amazon-web-services - 命令没有成功退出 aws cloudformation package --template template.yml exit status 255
- python - Selenium instagram bot:在该点不可点击 其他元素将收到点击
- javascript - (JS) 完整的菜鸟,将 *.json 代码导入变量
- python - 在上述目录中导入文件时出错
- php - 如何在php中随机组合两个字符串
- mysql - 在 SQL 中加入 3 个不同的表