RSS Amplifier

Blog

andrea simone costa

Recent content on andrea simone costa

andreasimonecosta.devRSS feed ↗14 posts

Latest posts

What the heck are reverse mapped types?

Introduction Link to heading Reverse mapped types are a powerful yet little-known feature of TypeScript that allow us to “run mapped types backward”. They are mainly a mechanism for inferring a function’s type parameters from values; however, the same inference steps can be performed at the type level using the infer keyword. The purpose of this article is to serve as a…

Parsing raw data with existential types

Disclaimer: parsing and validation of raw data is already well taken care of by libraries like Zod, effect/schema and many others. The goal of this article is simply to provide another perspective on existential types. The idea for this article came from here, where the technique I’m going to present is used to type a function from untyped to typed lambda calculus. Brief intro on existential…

What type parameters really are

Introduction Link to heading What are type parameters? Type parameters are just variables, plain and simple. It is no coincidence that they are referred to as type variables as well. Variables&hellip;really? Link to heading Yes! Let&rsquo;s take the following function definition as an example: function toPair<X>(x: X): [X, X] { return [x, x]; } We may be used to seeing toPair as a generic function…

How to express correlations

Introduction Link to heading In this article, I delve into the details of a semi-obscure and sufficiently complex yet quite powerful pattern for expressing correlations between different entities or within the same entity. Over time, I&rsquo;ve found it recommended multiple times to solve seemingly unrelated problems that, in reality, share a common root. The featured pattern is well presented in…

What the heck is a homomorphic mapped type?

Introduction Link to heading I remember back in the day when I stumbled upon the term homomorphic for the first time in the good ol&rsquo; TypeScript handbook. Honestly, the handbook&rsquo;s explanation was a bit fuzzy to me. After listing a couple of example mapped types: type Nullable<T> = { [P in keyof T]: T[P] | null }; type Partial<T> = { [P in keyof T]?: T[P] }; The handbook continued by…

Cloning JavaScript objects with graph theory

Introduction Link to heading The JavaScript slang allows us to clone objects in more than one way. We can perform a shallow clone using Object.assign or the spread syntax and a deep clone thanks to the JSON.parse(JSON.stringify()) trick. Unfortunately, the last solution suffers from a problem: JSON.stringify cannot work on an object that has circular references, erroring out in such a case. In…

Do Vue 3 refs admit a monad instance?

tl;dr Link to heading After an initial discussion about the possibility of refs admitting a monad instance, I present a function, useSwitchMap, that helps compose a ref with a function from values to refs. Another rising need in the Vue 3 ecosystem is the possibility to compose a ref with a function from values to objects containing refs, and useSwitchMapO will do the trick. Why monads? Link to…

JavaScript Iterators and Generators: Asynchronous Generators

Series: JavaScript Iterators and Generators Introduction Link to heading Here we are! It&rsquo;s time for Asynchronous Generators! Are you excited? I am excited 😃. This kinda exotic type of function was added to the language at the same time as async iterators were, and it answers the question: how async functions and generators should compose? If you think about it, async generators are still…

JavaScript Iterators and Generators: Asynchronous Iterators

Series: JavaScript Iterators and Generators Introduction Link to heading Fresh news of the JavaScript language (we are talking about ES2018), Asynchronous Iterators, and the corresponding Asynchronous Generators, landed to solve a subtle, but important, problem. We have seen that each iteration step performed with a synchronous iterator returns the {done, value} object, which is called iterator…

JavaScript Iterators and Generators: Synchronous Generators

Series: JavaScript Iterators and Generators Introduction Link to heading Generators are a very particular type of function. In JavaScript, we are used to waiting for the completion of a subroutine before being able to continue with the program. In other words, each function will run until the end of its body and no other code is able to interfere, running in between. Generators break this rule,…

JavaScript Iterators and Generators: Synchronous Iterators

Series: JavaScript Iterators and Generators Introduction Link to heading The first thing to point out is that iteration in JavaScript is based on a protocol: a set of conventions that replace what would have been interfaces in a language with support for interfaces. Anyway, the ECMAScript Specification mostly uses the word &ldquo;interface&rdquo;, therefore I will do the same. To whom this concept…

The shortest way to conditionally insert properties into an object literal

What am I ranting about? I&rsquo;m talking about this: const obj = { ...condition && { prop: value }, }; Trust me, this is perfectly acceptable and executable JavaScript. Surprised? Shaken up? Aghast? Maybe just intrigued? Bear with me for few lines, I&rsquo;ll try to explain what&rsquo;s going on thanks to the ECMAScript Language Specification. It won&rsquo;t be so boring, I new Promise().…

About Me

const myself = { first_name: 'Andrea', middle_name: 'Simone', surname: 'Costa', age: 27, country: 'Italy', nickname: 'jfet97', interests: ['functional programming', 'computer science'], occupation: 'Software Craftsman', email: 'andrysimo1997@gmail.com', github: 'github.com/jfet97', twitter: 'twitter.com/jfet97', } I hope my code is better than my description. My Communities Link to heading Vue.js…

Courses