typescript - 如何正确键入接收具有多个键:值对的对象的函数?
问题描述
我有一个对象,其中包含一些用户会话值:
const userSession = {
token: '',
full_name: '',
username: '',
password: '',
role_name: '',
gender: '',
}
我有一个函数必须更新这个对象中的一个或多个值,但我很难指定具有未知数量值的对象类型。感觉必须用泛型来完成,但我对它们没有太多经验。例如,如果调用
updateSession({token: 'some new token value', gender: 'new gender'})
should return {
token: 'some new token value',
full_name: '',
username: '',
password: '',
role_name: '',
gender: 'new gender',
}
我试过的:
const updateSession = <T extends {}>(objectParam: T) => {
const updatedUserObject = { ...userSession }
for (const [key, value] of Object.entries(objectParam)) {
console.log(`${key} ${value}`)
updatedUserObject[key] = value
}
return updatedUserObject
}
但这根本没有多大意义……
现在使用@obe 的解决方案可以让我Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'UserSessionInterface'. No index signature with a parameter of type 'string' was found on type 'UserSessionInterface'
知道我的功能和界面在哪里:
export interface UserSessionInterface {
token: string
email?: string
full_name?: string
username?: string
password?: string
role_name?: string
gender?: string
imageUri?: string
}
const session: UserSessionInterface = {
token: '',
full_name: '',
username: '',
password: '',
role_name: '',
gender: '',
}
const updateSession = (objectWithUpdates: UserSessionInterface) => {
const updatedUserObject: UserSessionInterface = { ...session }
for (const [key, value] of Object.entries(objectWithUpdates)) {
console.log(`${key} ${value}`)
updatedUserObject[key] = value
}
try {
await EncryptedStorage.setItem(
'user_session',
JSON.stringify(updatedUserObject),
)
console.log('successfully updated data')
console.log(updatedUserObject)
setSession(updatedUserObject)
} catch (error) {
console.error(error)
}
}
我已经修复了updatedUserObject[key as keyof IEmptySession] = value
,所以我现在想知道,这是一个有效的修复还是有更好的方法?出现该错误的TSPlayground
解决方案
您可以将对象声明为带有(一些?)可选键的接口:
interface IEmptySession {
token: string,
full_name?: string,
username?: string,
password?: string,
role_name?: string,
gender: string,
}
...
const updateSession = (objectParam: IEmptySession): IEmptySession => {
const updatedUserObject: IEmptySession = { ...objectParam }
for (const [key, value] of Object.entries(objectParam)) {
console.log(`${key} ${value}`)
updatedUserObject[key] = value
}
return updatedUserObject
}
如果您想允许任何键,那么您可以像这样定义接口:
interface IEmptySession {
[index: string]: string;
}
推荐阅读
- c# - 条件数据注释和下拉视图模型未正确验证
- python - 创建一个循环以找出前 20 天内的销售数量
- electron - 在 Electron 中使用时,Workbox js 没有缓存
- sql - 同一列上主键上的聚集和非聚集索引如何提高性能
- javascript - 当我们使用 new 关键字时与创建对象的过程混淆
- r - 使用 R 更改特定列中的数据
- json - 返回的 Json 响应是否保留列表的插入顺序?
- html - 用于移动作品的 CSS 下拉导航栏,但在 iphone 上不可见
- jupyter-lab - Beakerx - '验证发现的问题:beakerx/tree beakerx/extension'
- php - 如何计算在 laravel 中使用 DB::raw() 的位置?