Skip to content
TS18047

    ↑↓ move · ⏎ open · esc close

    TS18047

    Value is possibly null

    'value' is possibly 'null'.

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

    A nullable value is used as though the `null` branch had already been ruled out.

    Reproduction

    function upper(value: string | null) {
      return value.toUpperCase()
    }

    The build asserts this emits exactly this code.

    Why the compiler says this

    With strict null checking, `string | null` is two real possibilities. Methods of `string` are safe only after control flow proves that the current value belongs to the string branch.

    Fixes

    1. 01
      function upper(value: string | null) {
        if (value === null) return ''
        return value.toUpperCase()
      }

      Guard the null case and choose the behavior your domain requires.

    2. 02
      function upper(value: string | null) {
        return value?.toUpperCase() ?? ''
      }

      Use optional chaining with an explicit fallback for a compact transformation.

    Takeaway

    Narrow nullable values where you use them; a non-null assertion merely moves the risk to runtime.

    Where to go next

    Errors