Search code examples
typescripttypestypeerrortslinttypescript-eslint

How to access nullable typescript nested type


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)

Solution

  • type Query = { a: { b?: { c: string } } };
    
    type foo = NonNullable<Query['a']['b']>['c'];
    

    Playground