除了使用“删除”之外,还有其他方法可以从对象中删除属性吗?

时间:2019-05-08 10:58:37

标签: javascript typescript properties preserve

我对Javascript或Typescript中delete关键字的行为有疑问。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

我需要的是一种从对象中选择属性的方法,以及从对象中忽略属性的方法。

Typescript带有内置Pick类型的构建 https://www.typescriptlang.org/docs/handbook/advanced-types.html

type Pick<T, K extends keyof T> = { [P in K]: T[P]; }

Pick的对立面是Omit,可以这样实现:

export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof 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 omitMany = <T, K extends keyof T>(entity: T, props: K[]): 
      Omit<T, K> => {
  return props.reduce((s, prop) => (delete s[prop] ,s), entity)
 }

对于Omit,我使用delete关键字,一开始它似乎可以工作,但现在遇到了一些问题。主要问题是针对omitMany修改了原始对象。 Wich在我的程序中保存原始数据和保存状态会引起问题。

我写了一个简单的例子来说明我的问题:

// First I make some interface that contains some structure for data
interface SomeObject { x: string, y: number, z: boolean }

// Initialize object1 with properties x, y and z, containing the important data
// I want to preserve all the data in object1 throug the entire program
let object1: SomeObject = { x: "something", y: 0, z: false }
// I can print all properties of object1
console.log(`Object 1: x = ${object1.x}, y = ${object1.y}, z = ${object1.z}`) 
// OUTPUT: "Object 1: x = something, y = 0, z = false"

// omit or delete property 'x' from object 1, defining object 2
let object2 = omitMany(object1, ["x"]) // The type of object2 is: {y: number, z: boolean}
// Here I can only get properties z and y, because x has been omited
// Calling: object2.x gives an compile error
console.log(`Object 2: y = ${object2.y}, z = ${object2.z}`)
// OUTPUT: Object 2: y = 0, z = false (as expected)

// Everything works fine from here, but...
// When I recall omitMany on object1 the following happens: 

// Initialize object3 from object1, removing 'x' from an object where x = undefined
let object3 = omitMany(object1, ["x"]) // This code compiles, omiting 'x' from object2 gives an compiler error
//Printing object3 does show no problems, since it satisfies the expected result. Remove 'x' and keep 'y' and 'z'
console.log(`Object 3: y = ${object3.y}, z = ${object3.z}`)
// OUTPUT: Object 3: y = 0, z = false

// But when I print object1 again
console.log(`Object 1: x = ${object1.x}, y = ${object1.y}, z = ${object1.z}`) 
// OUTPUT: Object 1: x = undefined, y = 0, z = false 
// We lost the data of 'x'!!!


// I also ran into problems when I try to pick property 'x' from the original object1 
let object4 = pickMany(object1, ["x"]) // The type of object4 is {x: string}
// When I print 'x' from object4 it is still undefined
console.log(`Object 4: x = ${object4.x}`) 
// OUTPUT: Object 4: x = undefined

我知道这与delete的行为有关,但是还有另一种方法可以从对象中删除属性而不丢失原始对象的信息吗?因此保留所有的值和属性。

可以使用临时变量解决此问题,但我首先想看看是否还有其他解决方案。

1 个答案:

答案 0 :(得分:1)

这是我解决此问题的方法:

// https://stackoverflow.com/a/49579497/14357
/** Extracts optional keys from T */
export type OptionalKeys<T> = {
    [K in keyof T]-?: ({} extends {
        [P in K]: T[K];
    } ? K : never);
}[keyof T];

/** Typesafe way to delete optional properties from an object using magic of OptionalKeys<T> */
export const deleteOptionalProperty = <T>(obj: T, id: OptionalKeys<T>): T => {
    const { [id]: deleted, ...newState } = obj;
    return newState as T // this type-conversion is safe because we're sure we only deleted optional props
}

export const deleteOptionalProperties = <T>(obj: T, ...ids: OptionalKeys<T>[]): T =>
    ids.reduce((prev, id) => deleteOptionalProperty(prev, id), obj)