Now I know what pick does But I want to do the opposite and remove values from an object

0 votes

Now I know what 'pick' does. But I want to do the opposite and remove values from an object.

I want it to work similar to this:

type MyObject = {

    k0: number,
    k1: string,
    k2: boolean,
    k3: number[] 

}

let obj: Remove<MyObject, 'k1'|'k2'>

Required: { k1:number, k2:boolean }

May 27, 2022 in TypeSript by Logan
• 2,140 points
813 views

1 answer to this question.

0 votes

You can use the keyword Omit for this task. This has been done since TypeScript 3.5.

type ObjectType = {
    k0: number,
    k1: string,
    k2: boolean,
    k3: number[]
}

let obj: Omit<ObjectType, 'k1' | 'k2'> // type is { k0: number, k2: boolean }

For older versions of TypeScript, you could also use a combination of pick and exclude.

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
answered May 27, 2022 by Nina
• 3,060 points

Related Questions In TypeSript

0 votes
1 answer

What does the ! (exclamation mark) do in TypeScript?

The exclamation mark is called the non-null ...READ MORE

answered Jun 1, 2022 in TypeSript by Nina
• 3,060 points
1,230 views
0 votes
1 answer

What does the @ (at sign) sign do in TypeScript?

The big news this week is the ...READ MORE

answered Jun 10, 2022 in TypeSript by Nina
• 3,060 points
1,334 views
0 votes
1 answer

Using TypeScript, and Object.assign gives me an error "property 'assign' does not exist on type 'ObjectConstructor'"

You can use type assertion, like this: (<any>Object).as ...READ MORE

answered Aug 3, 2022 in TypeSript by Abhinaya
• 1,160 points
5,001 views
0 votes
1 answer
0 votes
1 answer

TypeScript Object assign gives me an error property assign does not exist on type ObjectConstructor

For TypeScript 2.1 and higher, you can ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
2,310 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
775 views
0 votes
1 answer

What does <T> do in TypeSript?

This is a TS Generics declaration. T will be declared ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
652 views
0 votes
0 answers

React Typescript: It looks like you're trying to use TypeScript but do not have typescript installed

I want to create a React application ...READ MORE

Jun 22, 2022 in TypeSript by Logan
• 2,140 points
1,044 views
0 votes
1 answer

Conversion from both keys and values of object

You could just change the declaration of ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
703 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 11,242 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