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
634 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,150 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,093 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
4,582 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,086 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
557 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
566 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, 2022 in TypeSript by Nina
• 3,060 points
526 views
0 votes
1 answer
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