3 Advanced TypeScript features
3 features of TypeScript I found cool.

Just a guy who loves to write code and watch anime.
Search for a command to run...
3 features of TypeScript I found cool.

Just a guy who loves to write code and watch anime.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

When it comes to TypeScript, there are three features I wanted to share with you that I found pretty cool.
For my examples, I used TypeScript Playground.
Conditional types were something I found extremely cool, being able to conditionally return a type, I actually found it hard to learn due to misunderstanding the term extends in this context.
Let's have a look at a simple example:

Here all we are saying is, if Dog is of type object, if it contains that, then we want the type NumberOrString to be a number, otherwise, if that wouldn't be the case for some reason, then we want the type to be a string.
Here you can read more on conditional types.
Template Literal Types are just the pure definition of flexibility, I was actually astonished when I learned about them ๐.
Let's say we want to define some CSS values, and we want developers to only be able to pass px or rem values, then we can do something like this:

Here we have two correct variables and one which is false, the one TypeScript complains about. The variables with the type Value must explicitly have a number and unit of either px or rem.
Here you can read more on template literal types.
Let's say we have a simple function, arrayOfNumbers, that returns an array of numbers with the given inputs, but the type of the inputs is unknown.

Using asserts of TypeScript we can create a function that we can invoke inside of arrayOfNumbers, in order to ensure the inputs are of type number, that way TypeScript won't complain anymore.

Now the function arrayOfNumbers doesn't complain, because using our assertIsNumber function, we ensure the inputs of the function are of type number, otherwise, we throw an error if that is not the case.
We created a function which asserts that the inputs are of type number, but you can do the same with type string, boolean and other types ๐.
Here you can find more examples of assert functions.