Home > database >  How to access nullable typescript nested type
How to access nullable typescript nested type

Time:10-26

I want to extract a nested type like in the code below. Is there a way to do that for a nullable field?

type Query = { a: { b?: { c: string } };

type c = Query['a']['b']['c'];

Property 'c' does not exist on type '{ c: string; } | undefined'.ts(2339)

CodePudding user response:

type Query = { a: { b?: { c: string } } };

type foo = NonNullable<Query['a']['b']>['c'];

Playground

  • Related