Hi All,
Is there any way to use Terraform CDK w/ Typescript without using Classes.
Hi All,
Is there any way to use Terraform CDK w/ Typescript without using Classes.
Hey, I wondered the same when I started and I never got around implementing it. Thanks to you I now have an occasion to give this a try and this is what I came up with:
// This is a utility function that makes a generated provider functional
const functional =
<ProviderType extends Record<string, any>>(provider: ProviderType) =>
(s: Scope) =>
new Proxy(provider, {
get(target, prop: any) {
return (name: string, options: any) =>
new target[prop](s, name, options);
},
}) as {
[Property in keyof ProviderType]: (
name: string,
opts: ConstructorParameters<ProviderType[Property]>[2]
) => ProviderType[Property];
};
// You apply it to a provider to get a functional version
const fnAws = functional(aws);
// and to a scope to get a scoped down version on that you can call functions that return class instances
const s3Bucket = fnAws(scope).S3Bucket("foo", { foo: "bar" });
Here is a TS Playground link, please let me know if it helps you and if there are edge cases with it.