Skip to content

    ↑↓ move · ⏎ open · esc close

    TS18046

    It is unknown

    'value' is of type 'unknown'.

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

    `unknown` is the type that refuses to be used until you prove what it is. That refusal is the entire feature.

    Reproduction

    declare const value: unknown
    
    value.trim()

    The build asserts this emits exactly this code.

    Why the compiler says this

    `any` says "stop checking"; `unknown` says "check first". They sit at the same place in the hierarchy — everything is assignable to both — but `unknown` is assignable to nothing without a narrowing step. This error is not an obstacle to route around with a cast: it is the type asking the one question a value from outside your program should always be asked.

    Fixes

    1. 01
      declare const value: unknown
      
      if (typeof value === 'string') {
        value.trim()
      }

      Narrow it. Inside the guard the compiler knows it is a `string`, and the method call is checked like any other.

    2. 02
      function isString(input: unknown): input is string {
        return typeof input === 'string'
      }
      
      declare const value: unknown
      
      if (isString(value)) {
        value.trim()
      }

      Or write the check once as a type predicate and reuse it. The `input is string` return type is what turns a boolean function into something the compiler will narrow on.

    Takeaway

    Reach for `unknown` at every boundary — parsed JSON, a caught error, anything from the network — and let this error force the check you would have skipped.

    Errors