Why TypeScript for Infrastructure?

Feb 11, 2022

One of the most popular languages for infrastructure-as-code is becoming Typescript. AWS CDK, Hashicorp CDK, Pulumi, and more support Typescript as a first-class citizen. How did we go from writing frontend components to cloud development kits? A technical look at the requirements of infrastructure-as-code languages.

  • A strongly typed system is useful for Infrastructure-as-code

AWS has over 200 different services and plenty of options for each. A strongly typed language helps developers catch wrong configurations before deploying ("compile time"). Despite being one of the most popular languages, we don't see as much Python used anymore for infrastructure-as-code. Ansible ran scripts with Python but was heavily configured in YAML.

  • It's all about the flexibility of the type system.

Type systems can be structural vs. nominal. Nominal typing means that two variables are type-compatible if-and-only if their declarations name the same types. Some examples are C, C++, Java, and Rust. Nominal type systems provide more type-safety at the cost of flexibility. On the other hand, structural typing means that two variables are type compatible if each feature in one variable corresponds to an identical feature in the second. You might know structural typing as 'duck typing' (the term doesn't deal with type equivalence), but:

if it looks like a duck, quacks like a duck, and swims like a duck, then it is probably a duck.

If a goose looks like a duck, quacks like a duck, and swims like a duck, then a goose is a duck. Typescript is a structurally typed language. These two types are equivalent in Typescript.

interface Dog {
  name: string;
}

interface Cat {
  name: string;
}
  • Interpreted languages vs. compiled languages are better for infrastructure-as-code.

Historically infrastructure tooling has been in interpreted languages. For example, bash and Ruby ("interpreted" depends on implementation) were the lingua franca of the last generation of infrastructure-as-code. This is because the applications aren't long-running and don't require complex runtime dependencies (that would be nice to statically link). And — Every sufficiently advanced configuration is wrong.

  • The type of developer deploying infrastructure is changing.

More often than not, the developer is deploying the code. DevOps-savvy engineers are both application developers and can manage cloud infrastructure. The last two generations of infrastructure configuration management (Chef/Puppet DSLs and YAML) weren't designed for application developers. Now there are enough abstractions – cloud APIs are just becoming palatable to the average developer.


1 The TypeScript Type system is actually Turing complete. This means that you can write a language inside the type system. See other Accidentally Turing Complete things.