{"id":337648,"date":"2021-04-13T07:22:04","date_gmt":"2021-04-13T14:22:04","guid":{"rendered":"https:\/\/css-tricks.com\/?p=337648"},"modified":"2021-04-13T07:22:06","modified_gmt":"2021-04-13T14:22:06","slug":"css-is-a-strongly-typed-language","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/css-is-a-strongly-typed-language\/","title":{"rendered":"CSS is a Strongly Typed Language"},"content":{"rendered":"\n

One of the ways you can classify a programming language is by how strongly or weakly typed<\/a> it is. Here, \u201ctyped\u201d means if variables are known at compile time. An example of this would be a scenario where an integer (1<\/code>) is added to a string containing an integer (\"1\"<\/code>):<\/p>\n\n\n\n

result = 1 + \"1\";<\/code><\/pre>\n\n\n\n

The string containing an integer could have been unintentionally generated from a complicated suite of logic with lots of moving parts. It could also have been intentionally generated from a single source of truth.<\/p>\n\n\n\n

Despite the connotations that the terms \u201cweak\u201d and \u201cstrong\u201d imply, a strongly-typed programming language isn\u2019t necessarily better than a weakly-typed one. There may be scenarios where flexibility is needed more than rigidity, and vice-versa. As with many aspects of programming, the answer is dependent on multiple external contexts (i.e \u201cit depends\u201d).<\/p>\n\n\n\n\n\n\n\n

The other interesting bit is there is no formal definition of what constitutes strong or weak typing. This means that perceptions of what is considered a strongly or weakly-typed language differ from person to person, and may change over time.<\/p>\n\n\n

TypeScript<\/h3>\n\n\n

JavaScript is considered a weakly-typed language, and this flexibility contributed to its early adoption on the web. However, as the web has matured and industrialized, use cases for JavaScript have become more complicated.<\/p>\n\n\n\n

Extensions like TypeScript<\/a> were created to help with this. Think of it as a \u201cplugin\u201d for JavaScript, which grafts strong typing onto the language. This helps programmers navigate complicated setups. An example of this could be a data-intensive single page application used for e-commerce.<\/p>\n\n\n\n

TypeScript is currently very popular<\/a> in the web development industry, and many new projects default to using TypeScript when first setting things up.<\/p>\n\n\n

Compile time<\/h3>\n\n\n

Compile time<\/a> is the moment when a programming language is converted into machine code. It is a precursor to runtime<\/a>, the moment when machine code is performed by the computer.<\/p>\n\n\n\n

As with many things on the web, compile time is a bit tricky. A setup that utilizes TypeScript will stitch together component pieces of JavaScript code and compile them into a single JavaScript file for the browser to read and run.<\/p>\n\n\n\n

The time when component pieces compile is when they are all combined. TypeScript serves as a kind of overseer, and will yell at you if you try to break the typed conventions you have set up for yourself before combination occurs.<\/p>\n\n\n\n

\"A
A sample TypeScript error in VS Code.<\/figcaption><\/figure>\n\n\n\n

The stitched-together JavaScript file is then ingested by the browser, which has its own compile time<\/a>. Browser compile time is highly variable, depending on: <\/p>\n\n\n\n