You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This does not behave as expected because the value is the string literal"true", not the boolean value true. This should be made more intuitive, or throw a warning?
data"coder_parameter""ai" {
name="ai"display_name="Use AI?"description="Do you want to use AI for this workspace?"type="bool"form_type="checkbox"default=falseorder=0
}
data"coder_parameter""ai_prompt" {
count=data.coder_parameter.ai.value==true?1:0name="prompt"display_name="Prompt"description=""type="string"form_type="textarea"default=""styling=jsonencode({
placeholder ="hello world"
})
}
While it might be surprising that the type isn't plumbed through the datasource (my assumption here is that we can't produce a constant schema in the provider and are leaning on maps to carry the values), The big trick is to not test for equality. If the line was
count=data.coder_parameter.ai.value?1:0
Terraform should do automatic type conversion, converting it into a boolean on the fly. However, it will specifically not do it in the equality case.
Where possible, Terraform automatically converts values from one type to another in order to produce the expected type. If this isn't possible, Terraform will produce a type mismatch error and you must update the configuration with a more suitable expression. Automatic type conversion does not occur when using the equality operator.
Terraform automatically converts number and bool values to strings when needed. It also converts strings to numbers or bools, as long as the string contains a valid representation of a number or bool value.
true converts to "true", and vice-versa false converts to "false", and vice-versa
It's a bit of an anti-pattern to explicitly test values against bool constants because TF lets you flex a bit with automatic type conversion. That said, I'm with y'all; folks are going to run into this just because some of the nuances of Terraform aren't super obvious.
Activity
[-]Using `bool` parameters not intuitive[/-][+]Using `bool` parameter values not intuitive[/+]bpmct commentedon Apr 22, 2025
I imagine many folks will run into this confusion. Huge fan of some form of warning or "magic" handling
angrycub commentedon Aug 4, 2025
While it might be surprising that the type isn't plumbed through the datasource (my assumption here is that we can't produce a constant schema in the provider and are leaning on maps to carry the values), The big trick is to not test for equality. If the line was
Terraform should do automatic type conversion, converting it into a boolean on the fly. However, it will specifically not do it in the equality case.
Ref: https://developer.hashicorp.com/terraform/language/expressions/types#type-conversion
It's a bit of an anti-pattern to explicitly test values against bool constants because TF lets you flex a bit with automatic type conversion. That said, I'm with y'all; folks are going to run into this just because some of the nuances of Terraform aren't super obvious.