1

Is it possible in typescript to deeply remove a prop from a class in an array nested in another class? this way for example:

class Nested {
  propX!: string;
  propY!: string;
  propZ!: string;
}

class Parent {
  propA!: string;
  propB!: number;
  propC!: string;
  nesteds!: Nested[]; 
}

// remove propZ from nesteds in Parrant class
class ParentInput implement Exclude<Parent, 'propC'|'nesteds.propZ'> {
  //...
}


2
  • Do you need it recursive or you know that nesteds occurs only at the top level? Commented Jun 16, 2021 at 15:27
  • @A.Chiesa I don't need it recursive. Commented Jun 16, 2021 at 21:42

1 Answer 1

1

You can use Pick and Exclude to filter out properties. Since Nested is, well, nested, you'll have to use extra type declarations to get it working:

// Grab every property except for 'propZ' out of Nested.
//   This is optional: you can just use `Pick<Nested...` directly below.
type FilteredNested = Pick<Nested, Exclude<keyof Nested, 'propZ'>>

// Grab every property except for 'propC' and 'nesteds' from Parent, 
//   then intersect a new `nesteds` definition using the above type
type FilteredParent = Pick<Parent, Exclude<keyof Parent, 'propC' | 'nesteds' >> & {
    nesteds: FilteredNested[]
}

class ParentInput implements FilteredParent {
    nesteds!: FilteredNested[];
    propA!: string;
    propB!: number;
}

Full Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.