簡體   English   中英

Typescript:使用相同的鍵但不同的值創建 object

[英]Typescript: Create object with same keys but different values

我有以下初始 object

const initialValues = { name: 'Jon', email: 'jon@me.com' }

我想創建一個相同的 object ,除非所有值都是boolean並且默認為false 像這樣

const expected = { name: false, email: false }

我創建了以下 function 可以滿足我的要求

const expected = cloneWithDefaults<typeof initialValues>(initialValues)

function cloneWithDefaults<T>(values: T) {
  type U = { [K in keyof T]: boolean }
  const keys = Object.keys(values) as Array<keyof T>
  const partial: Partial<U> = keys.reduce<Partial<U>>((acc, cur) => {
    acc[cur] = false
    return acc
  }, {})
  const final = partial as U
  return final
}

我還創建了不使用 reduce 的第二個版本

function createCloneWithDefaultsV2<T extends { [key: string]: unknown }>(values: T) {
  type U = { [K in keyof T]: boolean }
  const keys = Object.keys(values) as Array<keyof U>
  const partial: Partial<U> = {}
  for (let k in keys) {
    partial[k] = false
  }
  const final = partial as U
  return final
}

我想知道是否有更好/更簡潔的方法來做到這一點。 特別是我想as擺脫這兩種用途。

在 v2 中,我想知道是否有任何偏好unknown而不是any

這是我的寫法:

    function cloneWithDefaults<T>(values: T) {
        return <{[key in keyof T]: boolean}> Object.entries(values).reduce((p, [k, v]) => Object.assign(p, { [k]: false }), {});
    }
    const initialValues = { name: 'Jon', email: 'jon@me.com' };
    const expected = cloneWithDefaults(initialValues);

由於reduce()調用,沒有強制轉換,我看不到任何選項。 (無論是部分引用還是任何引用。)請注意,Object.entries()要求downlevelIteration ts編譯器選項為true( https://www.typescriptlang.org/docs/handbook/compiler-options.html )。

這是基於克里斯多夫的答案的另一種選擇

const cloneWithDefaultValues = <T>(input: T) => Object
    .keys(input)
    .reduce(
        (clone, key) => ({ [key]: false, ...clone }),
        {} as Record<keyof T, boolean>
    );

您還可以使用默認值編寫 class ,以便:

class YourObject {
    name: string | boolean = false;
    email: string | boolean = false;

    constructor(
        name: string | boolean = false,
        email: string | boolean = false
    ) {
        this.name = name;
        this.email = email;
    }
}

// now, you can create an object with no parameters:

let obj = new YourObject();
console.log(obj);
// output:
//      Object { name: false, email: false }

// then assign whatever strings to its fields:
obj.name = "Jon";
obj.email = "jon@mail.com";

// or create a new one with known values:
let jon = new YourObject("Jon", "jon@me.com");
console.log(jon);
// output:
//      Object { name: "Jon", email: "jon@me.com" }

這可能是一種僅使用一個as

function cloneWithDefaults<T>(values: T) {
     const exp: {[key in keyof T]: boolean} = Object.entries(values).reduce((p, [k, v]) => Object.assign(p, { [k]: false }), {} as {[key in keyof T]: boolean});
        return  exp;
    }
    const initialValues = { name: 'Jon', email: 'jon@me.com' };
    const expected = cloneWithDefaults(initialValues);
    console.log(">>>>>>", expected)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM