如何省略“删除"对象的许多属性? [英] How to Omit "DELETE" many properties from an Object?

查看:49
本文介绍了如何省略“删除"对象的许多属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个方法返回以下类型 PickOmit 其中 Omit 是 type Omit= Pick.从对象中删除多个属性时,我遇到了一些麻烦.

I have two methods that return the following types Pick<T, K> and Omit<T, K> where Omit is type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>. I have some trouble when it comes to removing multiple properties from an object.

我有一个方法 pickOne 从对象中选择一个属性,一个方法 pickMany 从一个对象中选择多个属性,还有一个方法 omitOne 从一个对象中删除一个属性目的.我想要一个 OmitMany 方法来从对象中删除多个属性,但是在修复方法中的类型错误时我卡住了.

I have a method pickOne that selects one property from an object, a method pickMany that picks multiple properties from an object and a method omitOne that removes one property from an Object. I would like to have a OmitMany method to remove multiple properties from an Object, but I got stuck when fixing the type errors in the method.

方法的实现:

export let pickOne = <T, K extends keyof T>(entity: T, props: K ): Pick<T, K> => {
    return { [props] : entity[props] } as Pick<T, K>
}

export let pickMany = <T, K extends keyof T>(entity: T, props: K[]) => {
   return props.reduce((s, prop) => (s[prop] = entity[prop], s) , {} as Pick<T, K>)
}

export let omitOne = <T, K extends keyof T>(entity: T, prop: K): Omit<T, K> => {
    const { [prop]: deleted, ...newState} = entity
    return newState
}

// And the OmitMany for so far I tried, the problem is with storing the entity
// in a temporary variable. This function only omits the last property in the
// the array. I would like an implementation simular to pickMany.
export let omitMany = <T, K extends keyof T>(entity: T, props: K[]): Omit<T, K> => {
    let result = entity as Omit<T, K>
    props.forEach(prop => {
        result = omitOne(entity, prop)
    })
    return result
}

我希望 omitMany({x: 1, y: 2, z: 3, r: 4}, ['x', 'y']) 的输出是输入 {z: number, r: number},但你知道输出是 {x: number, z: number, r: number} 类型的对象>

I expect the output of omitMany({x: 1, y: 2, z: 3, r: 4}, ['x', 'y']) to be an object of type {z: number, r: number}, but right know the output is an object of type {x: number, z: number, r: number}

推荐答案

您的问题与打字稿无关.omitMany 的类型按照您的预期运行,唯一的问题是在运行时它不会删除您希望它删除的所有属性,这是由于您调用 <entity 上的 code>omitOne 而不是之前的结果.不幸的是,这需要一些类型断言,但它会起作用:

Your issue has little to do with typescript. The types work out as you expect them to for omitMany, the only problem is that at run-time it does not remove all the properties you expect it to and this is cause by the fact that you call omitOne on entity instead of the previous result. This requires some type assertions unfortunately, but it will work:

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>

let omitOne = <T, K extends keyof T>(entity: T, prop: K): Omit<T, K> => {
    const { [prop]: deleted, ...newState } = entity
    return newState
}

let omitMany = <T, K extends keyof T>(entity: T, props: K[]): Omit<T, K> => {
    let result = entity as Omit<T, K>
    props.forEach(prop => {
        result = omitOne(result, prop as unknown as keyof Omit<T, K>) as Omit<T, K>
    })
    return result
}

let o = omitMany({ x: 1, y: 2, z: 3, r: 4 }, ['x', 'y'])
console.log(o)

游乐场

这篇关于如何省略“删除"对象的许多属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆