How to fix Error: Inconsistent conditional result types in Terraform
Terraform conditional expressions require both branches to return the same type. This error occurs when your ternary operator returns different types (e.g., string vs number), and Terraform's type system won't allow implicit conversion.
Quick fix
Fixes Error: Inconsistent conditional result types
Error: Inconsistent conditional result types
The true and false result expressions must have consistent types.
The 'true' value is string, but the 'false' value is number.- Identify the mismatched types
- Convert to a common type
- For list vs. single value mismatches
- For object/map structure mismatches
Error: Inconsistent conditional result typesThe true and false result expressions must have consistent types.The 'true' value is string, but the 'false' value is number.Error: Inconsistent conditional result types
What this error means
In Terraform, conditional expressions use the syntax `condition ? true_value : false_value`. Both the `true_value` and `false_value` branches must have the exact same type. When Terraform detects type mismatches between branches—such as a string in one branch and a number in another, or objects with different structures—it raises this error to prevent type ambiguity. This is a strict type-safety feature. Unlike some languages that allow implicit type coercion, Terraform requires explicit type compatibility. The error message typically specifies what types were detected in each branch, helping you identify the mismatch.
Typical symptoms
- Terraform plan or apply fails during expression evaluation
- Error message explicitly states which branch has which type (e.g., 'The true value is string, but the false value is number')
- The error occurs when using conditional expressions with variables, locals, or resource attributes
- Different object structures in conditional branches (e.g., different number of attributes)
Common causes
- Mixing strings and numbers in conditional branches (e.g., var.enabled ? 'yes' : 0)
- Using a list in one branch and a single value in another without wrapping
- Objects or maps with different attributes or structure between branches
- Mixing null with other types when Terraform cannot infer a unified type
- Using different resource types or incompatible nested structures in each branch
How to fix "Error: Inconsistent conditional result types"
Read the full error message carefully. Terraform tells you exactly which branch has which type:
Error: Inconsistent conditional result types
The true and false result expressions must have consistent types.
The 'true' value is string, but the 'false' value is number.Locate the conditional expression in your code that triggered this error.
Use Terraform's type conversion functions to ensure both branches return the same type. The most common conversions are:
Convert to string:
value = var.enabled ? "yes" : tostring(0)Convert to number:
value = var.enabled ? tonumber("42") : 0Convert to list:
value = var.enabled ? [var.item] : []Convert to map:
value = var.enabled ? { key = "value" } : {}Choose the conversion function that matches your desired output type.
If one branch returns a list and the other a single value, wrap both in lists:
# ❌ Wrong: mismatch between list and single value
items = var.use_list ? var.item_list : var.single_item
# ✅ Correct: both sides return lists
items = var.use_list ? var.item_list : [var.single_item]This ensures both branches produce the same type structure.
When objects have different attributes, explicitly define the same structure in both branches:
# ❌ Wrong: objects have different attributes
config = var.use_advanced ? {
name = "prod"
region = "us-east-1"
tags = { env = "prod" }
} : {
name = "dev"
}
# ✅ Correct: both have the same attributes
config = var.use_advanced ? {
name = "prod"
region = "us-east-1"
tags = { env = "prod" }
} : {
name = "dev"
region = "us-west-2"
tags = { env = "dev" }
}For more complex conditional logic with resource references, consider using splat expressions or try() to handle null values gracefully:
# Using splat for optional resource references
resource_ids = var.create_resources ? aws_instance.main[*].id : []
# Using try() to handle potential missing attributes
config = try(var.custom_config, var.default_config)These approaches help unify types when dealing with potentially missing resources or attributes.
Advanced notes
Type Inference in Terraform: Terraform's type system is stricter than many programming languages. It performs type inference but doesn't allow implicit coercion between incompatible types. This design choice prevents silent bugs from type mismatches.
Empty Collections and Ambiguity: Empty collections like {} or [] can be ambiguous. An empty object could be map(string) or map(any). When used in conditionals, Terraform may struggle to unify types. In such cases, use explicit type constructors or splat expressions.
Null Handling: Mixing null with other types in conditionals can cause issues because null is compatible with any type at the value level, but Terraform's type checker is strict. Use try(), coalesce(), or wrap values in collections to handle optional values.
Dynamic Blocks and Splat: When working with conditional resource creation, prefer splat expressions (resource[*].id) over conditionals, as they naturally handle the zero or many case and return consistent list types.
Frequently asked questions
- What does "Error: Inconsistent conditional result types" mean?
- In Terraform, conditional expressions use the syntax condition ? true_value : false_value. Both the true_value and false_value branches must have the exact same type.
- How do I fix "Error: Inconsistent conditional result types"?
- Terraform conditional expressions require both branches to return the same type. This error occurs when your ternary operator returns different types (e.g., string vs number), and Terraform's type system won't allow implicit conversion.
- What causes "Error: Inconsistent conditional result types"?
- Mixing strings and numbers in conditional branches (e.g., var.enabled ? 'yes' : 0) Using a list in one branch and a single value in another without wrapping Objects or maps with different attributes or structure between branches
- Is the "Error: Inconsistent conditional result types" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚Terraform Official Documentation
- 📖Terraform Troubleshooting Workflow Guide(Official Docs)
- 🐙