Go type check is too strict: `any[]` in TS does not allow passing `*[]*string` in Go
- Dominant language
- TypeScript
- Stars
- 2.9k
- Forks
- 267
- Avg merge
- 1d 25m
- Merged PRs (30d)
- 14
Description
### Describe the bug
Our CDKTF function `element` is defined in TypeScript as follows ([source](https://github.com/hashicorp/terraform-cdk/blob/4c7b678beacc7ea2c24d9d45b5161ea23fd6d05f/packages/cdktf/lib/terraform-functions.ts#L267)):
```ts
public static element(list: any[] | IResolvable, index: number) { ... }
```
And e.g. the `.names` property of a datasource ([source](https://github.com/cdktf/cdktf-provider-aws/blob/2ca62f27aab3503dab816e0adc3984ecfe3419c2/src/data-aws-availability-zones/index.ts#L360-L362)) might be returning a Token with the type `string[]` which is something that can be passed to `element()` in TypeScript.
However, this does not seem to work in Go because the type checking is a bit too strict.
The following example does not work:
```go
cdktf.Fn_Element(zones.Names(), jsii.Number(0))
```
and fails with:
```
panic: parameter list must be one of the allowed types: *[]interface{}, IResolvable; received &[]*string{(*string)(0xc00039c3b0)} (a *[]*string)
goroutine 1 [running]:
github.com/hashicorp/terraform-cdk-go/cdktf.Fn_Element({0x15d45a0, 0xc000011fb0}, 0xc00039e118)
/Users/ansgar/projects/hashicorp/terraform-cdk/packages/cdktf/dist/go/cdktf/cdktf_Fn.go:590 +0x106
main.NewFunctionsStack({0x5545008, 0xc000014110}, {0x1705f7b, 0x9})
/Users/ansgar/projects/hashicorp/terraform-cdk/examples/go/documentation/functions.go:24 +0x279
main.main()
/Users/ansgar/projects/hashicorp/terraform-cdk/examples/go/documentation/main.go:14 +0xf3
```
Whereas with this workaround ([source](https://github.com/hashicorp/terraform-cdk/blob/4c7b678beacc7ea2c24d9d45b5161ea23fd6d05f/examples/go/documentation/functions.go#L24)) it does work:
```go
cdktf.Fn_Element(cdktf.Token_AsAny(zones.Names()), jsii.Number(0))
```
It seems that these type checks ([source](https://github.com/hashicorp/terraform-cdk-go/blob/73f6ee51b0c2ac3eb878c8bd2e1dec32d5e2ba44/cdktf/cdktf_Fn__runtime_type_checks.go#L276-L298)) are too narrow:
```go
func validateFn_ElementParameters(list interface{}, index *float64) error {
if list == nil {
return fmt.Errorf("parameter list is required, but nil was provided")
}
switch list.(type) {
case *[]interface{}:
// ok
case []interface{}:
// ok
case IResolvable:
// ok
default:
if !_jsii_.IsAnonymousProxy(list) {
return fmt.Errorf("parameter list must be one of the allowed types: *[]interface{}, IResolvable; received %#v (a %T)", list, list)
}
}
if index == nil {
return fmt.Errorf("parameter index is required, but nil was provided")
}
return nil
}
```
### Expected Behavior
to work without the workaround
### Current Behavior
fails with error posted above
### Reproduction Steps
The example in [functions.go](https://github.com/hashicorp/terraform-cdk/blob/main/examples/go/documentation/functions.go) is available on `main` in the CDKTF repository. A shorter example can be supplied if required.
### Possible Solution
_No response_
### Additional Information/Context
_No response_
### SDK version used
jsii@1.73.0
### Environment details (OS name and version, etc.)
OS X
Contributor guide
Research direction
Reproduce the failure with examples/go/documentation/functions.go, then inspect the jsii runtime type-check generation and the referenced cdktf_Fn__runtime_type_checks.go validation logic. Compare the TypeScript any[] declaration in packages/cdktf/lib/terraform-functions.ts with the generated Go accepted types; done means the direct zones.Names() call works without Token_AsAny while existing IResolvable handling remains valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, typescript
- Domain
- developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100