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 in TypeSript by Logan
• 2,140 points
20 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 by Nina
• 3,020 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 in TypeSript by Nina
• 3,020 points
127 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 in TypeSript by Nina
• 3,020 points
23 views
0 votes
1 answer
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 in TypeSript by Nina
• 3,020 points
70 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 does <T> do in TypeSript?

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

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

Conversion from both keys and values of object

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

answered May 31 in TypeSript by Nina
• 3,020 points
16 views
0 votes
1 answer
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