Skip to content

    ↑↓ move · ⏎ open · esc close

    TS2564

    Property never assigned

    Property 'x' has no initializer and is not definitely assigned in the constructor.

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

    The class declares a property that is never given a value, so every instance would start out with `undefined` in a slot the type says is always a number.

    Reproduction

    class C {
      x: number
    }

    The build asserts this emits exactly this code.

    Why the compiler says this

    This comes from `strictPropertyInitialization`, and it closes a hole that existed for years: a declared property was trusted on its word. The compiler will accept an initializer, a definite assignment in the constructor, or an admission that the property is optional — what it will not accept is a type that claims more than the class can deliver.

    Fixes

    1. 01
      class C {
        x = 0
      }

      Initialize it inline. The type is inferred from the value, so there is nothing to keep in sync.

    2. 02
      class C {
        x: number
      
        constructor(x: number) {
          this.x = x
        }
      }

      Or assign it in the constructor, where the compiler can see every path reaches it.

    Takeaway

    The `!` definite-assignment modifier also silences this, and it is a promise the compiler cannot check. Reach for it only when something outside the class genuinely does the assigning.

    Errors