Skip to content

    ↑↓ move · ⏎ open · esc close

    TS18048

    Possibly undefined

    'maybe.a' is possibly 'undefined'.

    The compiler’s own words. Not translated — this is the string you pasted into a search box.

    An optional property was used without checking it. Under `strictNullChecks`, "optional" means the absence is part of the type and has to be handled.

    Reproduction

    declare const maybe: { a?: { b: number } }
    
    maybe.a.b

    The build asserts this emits exactly this code.

    Why the compiler says this

    Without `strictNullChecks` this compiles and throws at run time; with it, the gap becomes visible where it can still be fixed. The error is not asking you to prove the value exists — it is asking you to say what should happen when it does not, which is a question the code was silently answering with "crash".

    Fixes

    1. 01
      declare const maybe: { a?: { b: number } }
      
      const value = maybe.a?.b

      Optional chaining answers "nothing happens, the result is `undefined`" — which is right when the caller can cope with that.

    2. 02
      declare const maybe: { a?: { b: number } }
      
      if (maybe.a) {
        const value = maybe.a.b
      }

      A guard answers "we do nothing at all in that case", and narrows the type for the whole block rather than one access.

    Takeaway

    The non-null assertion `!` also silences this, and it is the one fix that answers the question with a promise instead of code. Keep it for the cases you can actually prove.

    Where to go next

    Errors