0

I have a nested object of optional properties. I found a way to declare this type via this answer. Now I'm trying to figure out how to apply this type to my object interfaces without having to modify every object that uses the interface. Some examples to make this clearer.

Here is a minimal reproducible example.

Here is the recursive object type:

export type PartialRecursive<T> = T extends object
? {
      [K in keyof T]?: PartialRecursive<T[K]>;
  }
: T;

Sample interface:

export interface Foo {
    bar: boolean;
    bax: boolean;
}

Simple object (NOTE: the array is required here):

import { Foo } from './types';
const foo: Foo[] = [
    {
        bar: true   
    }
]

To use the new interface I could do this:

import { Foo, PartialRecursive } from './types';
const foo: PartialRecursive<Foo>[] = [{...}];`

But I would prefer to apply this PartialRecursive type to the interface where it is declared, but I'm not sure how to do it. I have a lot of objects and would prefer to implement this type against the interface instead of each implementation that uses it.

I hope this is clear, checked the TS docs, but it was hard to find what I was looking for.

redonkulus
  • 21
  • 2
  • I'm not sure what your desired result is; are you trying to make it so that `Foo` has already had `PartialRecursive` applied to it, like [this](https://tsplay.dev/N5EMVN)? Or something else? As examples go this is strange because `Foo` and `PartialRecursive` are the same type; `Foo`'s properties are already optional. I think it would really help if you could provide a [mcve] that demonstrates the issue as clearly as possible, because as it stands... I'm confused. – jcalz May 31 '21 at 16:14
  • Sorry for being unclear. Foo's properties in my examples should not have had the optional flag on it. Your example is exactly what I want but TS is still giving me a type error when I tried to use it. For additional objects within the "foo" array, TS complains that there are still missing properties, it seems the "optional" part of PartialRecursive is not applying. – redonkulus May 31 '21 at 16:30
  • Are you wanting to avoid having to do `PartialRecursive[]`, you could just create another type `type FooRecursive = PartialRecursive[]; const foo: FooRecursive = [{}];` – Keith May 31 '21 at 16:32
  • "TS is still giving me a type error when I tried to use it". Please include a [mcve] of code that makes this happen, in the original post... preferably along with a link to a web IDE that demonstrates it. Otherwise, if it's just something happening in your own environment and you can't reproduce it externally, then you need to start giving details about your environment and how it differs from web IDEs... of course that puts others at a distinct disadvantage because only you have access to your environment. – jcalz May 31 '21 at 16:36
  • Got it, so I took your example and changed it to what I'm seeing locally, I hope this helps [example](https://www.typescriptlang.org/play?#code/HYQwtgpgzgDiDGEAEBRAHuGAbZBvAUPksUhGjAPYBOALkjQJ4zIAKItAliFgEoTwBXKlA4A3CAB4AKgD4kAXiRTSaGhGAATKEgoAjAFb86AfiS4kAbQDSSDsCQBrCAwoAzJQF1jALiRtO3HyCwmKSUtYeMgDcSAC+SL5SUYQktsBqVK4IyABCIFDIZGqa2v40XLz8QiLiEgSpqQU05cAA5lC+5gAMvlA0VHatcUgAZEh9A20WHsmpsXK4sSkkZJS0aRlZiEgAYhQUKsVaSHkFZkQNxLrsvrr7OCDAs5dI12i39xCPz8RLF8TwCjAPr0aA0Xx7CjTBSWf4NeovVLXKi+foCCBwuYAGkxJARiKuIHe9Co6NxvzhM3wsSAA). – redonkulus May 31 '21 at 16:39
  • Please modify your post to include the [mcve], as comments don't last forever and this info is necessary for someone trying to help you. I guess I kind of understand now that you want every interface that `extends Base` to automatically have `PartialRecursive` applied to it... but that is just not how TypeScript works at all. There's no mechanism to do this, and the structural type system and open object types really prevents this from even making much sense in TypeScript; you can always add required properties to an object type by extension, no matter what. – jcalz May 31 '21 at 16:45

0 Answers0