I don t know about return type in typescript

0 votes

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

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

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)

Jul 5 in TypeSript by Logan
• 2,140 points
9 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In TypeSript

0 votes
1 answer

How can I define a type for a CSS color in TypeScript?

There was a proposal for a type of ...READ MORE

answered Jun 15 in TypeSript by Nina
• 3,020 points
11 views
0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
13 views
0 votes
1 answer

What is "not assignable to parameter of type never" error in TypeScript?

All you have to do is define ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
839 views
0 votes
1 answer
0 votes
1 answer

How to create enum type in TypeScript?

TypeScript 0.9+ has a specification for enums: enum ...READ MORE

answered Jun 8 in TypeSript by Nina
• 3,020 points
30 views
0 votes
1 answer

How do I extend a TypeScript class definition in a separate definition file?

If you don't have control over the ...READ MORE

answered Jun 10 in TypeSript by Nina
• 3,020 points
170 views
0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11 in Others by narikkadan
• 11,280 points
722 views
0 votes
1 answer

How to use next-seo for setting nextjs meta tag with multiple OGP images?

https://github.com/garmeeh/next-seo use this git repo that contains ...READ MORE

answered Feb 24 in Others by narikkadan
• 11,280 points
464 views
0 votes
0 answers

how to sign bitcoin psbt with ledger?

I'm trying to sign a PSBT transaction ...READ MORE

Mar 9 in Blockchain by Soham
• 8,740 points
277 views
0 votes
0 answers

Can't bind to 'ngModel' since it isn't a known property of 'input'

I have this  input in my component ...READ MORE

Apr 29 in Other DevOps Questions by Kichu
• 19,040 points
149 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP