OK, this one is super useful, I promise! As you may already know, we can use toHaveBeenCalledWith on a spied function to check if that function has been called with a set of arguments. For example…
For the uninformed, optional chaining is a new JavaScript feature that allows you to access property values deep inside an object without checking that each reference in the chain is valid. For…
Sometimes we need to mock a FileList, for example, when we need to test file upload features. First, create a mock File object: The above function creates a file with a custom extension and size…
One approach to testing the localStorage function in Jest is to mock the setItem/getItem methods. To mock something with localStorage, we can do it via the Storage.prototype, for example: Now, you’ll…
An event system is a mechanism for globally receiving and broadcasting messages, which can be used in a Web Application to allow different components that do not have any parent-child relation to…
In the “Use String as Enum Key” article, we use the keyof typeof keywords to create a union of an enum’s keys. It might not be immediately clear why and how this keywords combination works. In this…
In TypeScript, you can define an enum and access its value with a string-like value, like this: In this example, the type of the key variable is not actually a string, but a literal type called…
Some notes about some not-so-well-known things about TypeScript Classes. In TypeScript, you can define a class’s properties in two ways: Define it as a class property in the normal way, or define it…
This article is inspired by the talk How the TypeScript compiler compiles, you should really check it out for a more in-depth understanding about the TypeScript’s compiling process. At a high level…
In the previous post, we learned how some utility types are implemented. The fact that they’re all done at the type level is very interesting. But not all utility types can be done like that. In…
TypeScript provides several utility types to help us manipulate types easier, like: Partial<T>, Required<T>, Pick<T, Keys>,… Internally, these types are implemented in the src/lib/es5.d.ts, at the…
In TypeScript, all the type definitions for the Web API are implemented in the lib.dom.d.ts file. One interesting thing is, this file is automatically generated from the Web Standards and does not…
So, I want to look into the TypeScript source code to understand how things work and maybe contribute to the project as well. This is a quite big project but I think the code organization is pretty…
One of the most confusing things in JavaScript is the context object this inside a function. The value of this depends on how the function is called, not how the function is defined. Let’s take a look…
By default, the TypeScript compiler tries its best to analyze the code and infer the type of your variables. Type Assertion is when you tell the compiler that you know a variable has a specific type…
Let’s say you are tasked to build the next Amazon, and you gotta start small. So, you build a website that only sells books and audiobooks for now. The system would have the BaseItem type, defining…
In TypeScript, an enum can be defined in various ways: a numeric enum, a string-based enum, or heterogeneous enum. By default, any enum member will have a numeric value: A string-based enum is an enum…
You can think of unknown as a type-safe version of any. They are both used to represent the type of value that is unknown beforehand (like a result of JSON.parse,…), but unknown required us to do more…
In Rust, there is a Result<T, E> type that could return either an Ok(T) value if the operation is succeeded, or an Err(E) value if the operation is failed. To mimic this type in TypeScript, first…
To use Jest for testing a TypeScript library, we need to install a couple of things: And set up the jest.config.ts file to use the ts-jest transformer when processing *.test.ts files: From now on, you…
With Parcel 2.0, it’s very easy to build a TypeScript library and publish them to NPM later on, with automatically generated types and built-in support for both CommonJS and ESModule. Let’s say you…
Let’s say, we are building a component to display some images (with lazy loading): And this component takes some properties (ImageProps) that came from the two imaginary types: The most inefficient…
Sometimes, you’ll run into a case where TS keeps complaining about a possibly undefined value. For example: The proper way to handle this error is actually to check if a is valid before returning any…