Urgent.News

What's breaking now, across thousands of outlets.

Tech

You Are Lying to Your Compiler. Use "satisfies" Instead of "as".

You are lying to the compiler. It is time to stop. I am going to say something that might make some of you uncomfortable. Every time you write as in TypeScript, you are basically telling the compiler to shut up and trust you. That feels powerful in the moment. It is also how you ship bugs to production. The as keyword does not check anything. It is a command, not a question. You are saying "I…

You are misguiding the compiler when you use the 'as' keyword in TypeScript. It is time to stop this behavior. The 'as' keyword tells the compiler to trust you, but it does not perform any checks, making it a command rather than a question. Sometimes, you may know better than the compiler, but most of the time, you don't. With TypeScript 4.9, a new operator called 'satisfies' has been introduced, which is better than 'as'.

The 'as' keyword overrides TypeScript's type inference, while 'satisfies' validates it. Using 'as', you can create an object that claims to be a certain type but may be missing required properties or have additional properties. This can lead to silent failures that TypeScript was supposed to prevent. The 'satisfies' operator, on the other hand, checks the validation of the type and preserves the precise type inferred by TypeScript.

For instance, if you misspell a property or forget to include it, TypeScript will throw an error immediately. Moreover, 'satisfies' keeps the literal type, ensuring that if you wrote a specific value, the object's property will be exactly that value, not just the type. Here's a real-life scenario where 'satisfies' changed how the author writes TypeScript.

Imagine defining a color palette with a specific type, 'ColorConfig'. With 'as', you lose precision, and TypeScript does not catch errors when an undefined property is accessed. However, with 'satisfies', TypeScript keeps the exact literal values, and if you attempt to access a property that does not exist, TypeScript will throw an error.

This behavior provides autocomplete for the actual keys, catches typos, and maintains the precise type. While there are legitimate cases where 'as' is needed, such as DOM queries or type guards, for most object literals, config files, and type validation, 'satisfies' is the better option. It is time to stop lying to the compiler and start validating.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Tree pattern matching

I managed to find a very effective decomposition of the pattern tree, which gives several useful properties for pattern matching.

Monthly update post

A short monthly progress report: Added the ability to select a folder/project before starting work. Added some of the missing mathematical elements.

More from Sunday 27 September →