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
583 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
6,598 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
1,310 views
0 votes
1 answer

Typescript Errors: How do I ignore the error "property does not exist on value of type"

to ignore it globally, in your tsconfig.json, ...READ MORE

answered Jul 26, 2023 in TypeSript by john

edited Mar 5, 2025 13,077 views
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
1,801 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
3,606 views
0 votes
1 answer

What do square brackets mean in a type definition in Typescript?

I've missed a basic type of TypeScript: Tuples. So ...READ MORE

answered Jun 22, 2022 in TypeSript by Nina
• 3,060 points
4,606 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
• 86,360 points
3,119 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
• 86,360 points
7,168 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,730 points
2,985 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
• 86,360 points
5,413 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