简体   繁体   English

我不知道打字稿中的返回类型

[英]I don't know about return type in typescript

TYPESCRIPT 3.4.3 TYPESCRIPT 3.4.3

I want to make function like this 我想做这样的功能

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

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

I don't know how to typing this function's return type 我不知道如何输入这个函数的返回类型

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;
};

If I use this function, return value still have types (With the exception of second string array parameter) 如果我使用这个函数,返回值仍然有类型(第二个字符串数组参数除外)

----------EDIT---------------- - - - - - 编辑 - - - - - - - -

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;
};

I don't know how to return. 我不知道该怎么回事。 Removing seconds parameter array property from first object 从第一个对象中删除秒参数数组属性

-----------EDIT 2---------------- -----------编辑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)

It shouldn't be hard I guess? 我觉得应该不难?

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')

Not sure if this is what you want, but without generics, you could use index signature to input and output an object with unknown keys. 不确定这是否是您想要的,但是如果没有泛型,您可以使用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"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于代码中的函数,我不知道它们对什么意味着什么 - about functions in code that I don't know what they mean for what 使用Axios,aynsc/await和typescript,不知道这个情况 - use Axios, aynsc/await and typescript, I don't know this situation 为什么 typescript 不警告这个 function 中的返回类型? - Why doesn't typescript warn about the return type in this function? 如果我不知道响应类型,可以执行XMLHttpRequest吗? - Can I perform an XMLHttpRequest if I don't know the response type? 在这种情况下如何使用泛型这种类型? 我不知道为什么 - how to use generic this type in this case ? i don't know why 我如何让打字稿停止抱怨它不知道的功能? - How do I get typescript to stop complaining about functions it doesn't know about? 为什么 typescript 不知道我检查了 object 类型? - Why typescript doesn't know that i checked for object type? 当我们不知道Typescript中的属性名称时,以编程方式推送到类型为any的数组 - Programmatically pushing to an array of type any when we don't know the property names in Typescript 如何将此 github 的内容实施到我的网站中? 我不太了解 javascript - How do I implement contents of this github into my website? I don't know much about javascript 我不知道有什么区别 - I don't know the difference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM