3

So this is a question to all Typescript wizards.

If I have an interface, e.g

interface A {
  value: string
  child : {
     nestedValue: number
  }
}

is it possible to derive a type that contains all properties of that interface A as string literal types? Like this:

type WhatIWant = 'value' | 'child.nestedValue'

Thanks!

abnormi
  • 628
  • 1
  • 7
  • 15
  • You would usually use `typeof A`, but I'm not sure if flattening nested objects would be possible at all. https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgILIN4FgBQzkBucANgK4QBcyAzmFKAOa74IAWwxAJsldnvvhARaETgDUS5KiFIBbAEbRmyAL641OXGACeABxQBrCNuoB5GOgC8yI9oD2MNAG4gA Why do you need `child.nestedValue` type? What do you want to use it for? – zerkms Jul 25 '20 at 07:29
  • @zerkms I want to write a type-safe function like this: `performMagic('child.nestedValue')` – abnormi Jul 25 '20 at 08:17
  • Even if you would be able to have a `child.nestedValue` type you wouldn't be able to use it as a properties path to traverse object type-safely. Assume you already have a value of `child.nestedValue` and try to implement the `performMagic`. – zerkms Jul 25 '20 at 08:49
  • Starting with Typescript 4.1 this will be possible https://stackoverflow.com/questions/58434389/typescript-deep-keyof-of-a-nested-object – abnormi Dec 12 '20 at 13:51

0 Answers0