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, 2022 in TypeSript by Logan
• 2,140 points
178 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, 2022 in TypeSript by Nina
• 3,060 points
3,325 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, 2022 in TypeSript by Nina
• 3,060 points
316 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, 2022 in TypeSript by Nina
• 3,060 points
48,634 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, 2022 in TypeSript by Nina
• 3,060 points
619 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, 2022 in TypeSript by Nina
• 3,060 points
2,155 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, 2022 in Others by narikkadan
• 63,420 points
1,918 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, 2022 in Others by narikkadan
• 63,420 points
5,121 views
0 votes
0 answers

how to sign bitcoin psbt with ledger?

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

Mar 9, 2022 in Blockchain by Soham
• 9,700 points
1,288 views
0 votes
1 answer

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

Just add this in the app.module.ts file: import { FormsModule ...READ MORE

answered Apr 30, 2022 in Other DevOps Questions by narikkadan
• 63,420 points
3,936 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP