# Fix terraform "Error: Inconsistent conditional result types"

**TL;DR:** The two sides of your `condition ? a : b` have different types. Terraform unifies both branches, so they must match structurally. Make both sides the same type: convert with `tostring()`/`tonumber()`, give both object branches the same attribute set, or restructure so the condition lives inside a `for` filter instead of wrapping two different shapes.

## The error

```text
Error: Inconsistent conditional result types

  on main.tf line 20, in locals:
  20:   widgets = var.preview ? local.filtered : local.unfiltered

The true and false result expressions must have consistent types. The 'true'
value is a tuple with 11 elements, but the 'false' value is a tuple with 12
elements.
```

## Steps

1. Read which side differs. The error spells it out: different tuple lengths, or `The 'true' value includes object attribute "X", which is absent in the 'false' value`.
2. For object branches with mismatched attributes, align the attribute sets. Add the missing attribute (even as null) to the smaller side:

   ```hcl
   # wrong: var.x != null ? { a = 1, b = 2 } : { a = 1 }
   # right: var.x != null ? { a = 1, b = 2 } : { a = 1, b = null }
   ```

3. For list-length mismatches, do not branch on two prebuilt lists. Build one list with a filtered comprehension:

   ```hcl
   widgets = [for w in local.all_widgets : w if var.preview ? w.preview : true]
   ```

4. For `jsondecode()` results, remember it returns an object, not a map. Wrap first: `{ for k, v in jsondecode(...) : k => v }`.
5. Run `terraform validate`. Expected: `Success! The configuration is valid.`

## When this applies

- `validate` fails with `Inconsistent conditional result types` on a ternary.
- One branch returns `{}` and the other returns a populated object, or branches differ in attribute sets or tuple lengths.

## When it does NOT apply

- `Inconsistent result after apply`. That is a provider bug at apply time, not a type error in your expression.
- Errors inside `dynamic` blocks about `each.value`. That is an iterator naming issue, not a conditional.

## Tool and version compatibility

- Terraform CLI 0.12+ through 1.x. Type unification rules unchanged.

## Why it happens

Terraform must know the expression's type without evaluating the condition, so it unifies both branches into one type. Objects unify only when their attribute sets match; tuples unify only at equal length. A mismatch is a static type error, not a runtime one, so it fails at validate.

## Edge cases and pitfalls

- The error fires in *both* variable values, because unification ignores which branch would be taken. You cannot hide a bad branch behind a condition that is currently false.
- `{}` vs populated object is the most common trigger. Decide the full attribute set up front and use nulls for the absent ones.
- `try()` does not help here; this is a type error, not an evaluation error.