【问题标题】:I don't know about return type in typescript我不知道打字稿中的返回类型
【发布时间】:2019-09-16 14:46:52
【问题描述】:

打字稿 3.4.3

我想做这样的功能

exportObjectUnit({ a: 1, b: 2, c: 3, d: 4 }, ['b', 'c'])

输出 { a: 1, d: 4 };

我不知道如何输入这个函数的返回类型

export const exportObjectKey = <T, K extends keyof T>(value: T, exports: K[]) => {
  const returnValue = {};

  Object.keys(value)
    .filter(key => {
      if (exports.indexOf(key) !== -1) {
        return false;
      }
      return true;
    })
    .map(key => {
      returnValue[key] = value[key];
    });

  return returnValue as T;
};

如果我使用这个函数,返回值仍然有类型(第二个字符串数组参数除外)

---------编辑----

export const exportObjectKey = <T>(value: T, exports: Array<keyof T>) => {
  const returnValue = {};

  Object.keys(value)
    .filter(key => {
      if (exports.indexOf(key as keyof T) !== -1) {
        return false;
      }
      return true;
    })
    .map(key => {
      returnValue[key] = value[key];
    });

  return returnValue as T;
};

我不知道如何返回。 从第一个对象中删除秒参数数组属性

-----------编辑 2----

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

export const exportObjectKey = <T, K extends keyof T>(value: T, omit: K): Omit<T, K> => {
  delete value[omit];
  return value;
};
export const exportObjectKeys = <T, K extends Array<keyof T>>(value: T, removes: K) =>
  removes.reduce((object, key) => exportObjectKey(object, key), value);

// This is not perfect version

const a = { a: 1, b: 2, c: 3 };
const keyOmitOne = exportObjectKey(a, 'b');
// When I type keyOmitOne.
// Type definition available, It works (a, c)

// ------------------------------------------

// But, when I use exportObjectKeys
const b = { a: 1, b: 2, c: 3 };
const keyOmitArray = exportObjectKey(b, ['b', 'c']);
// I thought type definition works (a available)
// But there is no definition in b value)

【问题讨论】:

  • 你为什么使用泛型?看来您确切地知道输入和输出是什么。泛型适用于必须使用不同类型的输入,然后输出相同类型的函数。在您的情况下,您不能为输入和输出类型写一个interfacetype 吗?
  • 每次调用输入对象和输出对象都不一样(参数变化)
  • value(和returnValue)不总是object 类型吗?您在value 上使用Object.keys() 并将returnValue 初始化为{}
  • 我不明白你的评论

标签: javascript typescript


【解决方案1】:

我猜应该不难吧?

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

function exportObjectUnit<T extends Object, K extends (keyof T)[]>(obj: T, ...keys: K): Omit<T, K[number]> {
    keys.forEach(k => delete obj[k])
    return obj
}

const t1 = exportObjectUnit({ a: 1, b: 2, c: 3, d: 4})
const t2 = exportObjectUnit({ a: 1, b: 2, c: 3, d: 4} , 'a')
const t3 = exportObjectUnit({ a: 1, b: 2, c: 3, d: 4} , 'a', 'b')
const t4 = exportObjectUnit({ a: 1, b: 2, c: 3, d: 4} , 'a', 'b', 'c')
const t5 = exportObjectUnit({ a: 1, b: 2, c: 3, d: 4} , 'a', 'b', 'c', 'd')

【讨论】:

  • 谢谢,这是最好的答案
【解决方案2】:

不确定这是否是您想要的,但如果没有泛型,您可以使用index signature 输入和输出具有未知键的对象。

type MyObject = { [key: string]: number }

function doSomething(obj: MyObject, arr: string[]) : MyObject {
    let a : MyObject = {a:3, b:4}
    return a
}

doSomething({a:4, b:4, c:4, d:2}, ["a", "d"])

【讨论】:

    猜你喜欢
    • 2020-06-29
    • 2021-12-17
    • 1970-01-01
    • 2021-09-18
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2017-05-20
    相关资源
    最近更新 更多