TypeScript Posts

23 posts in this category

Test parameter passed into a function with Jest
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…
TypeScript
Optional Chaining and Code Coverage
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…
TypeScript
Mocking File and FileList in Jest
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…
TypeScript
Testing localStorage in Jest
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…
TypeScript
Using the browser's CustomEvent
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…
TypeScript
Understanding keyof typeof
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…
TypeScript
String as enum key
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…
TypeScript
Some random notes about Classes
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…
TypeScript
Compilation Process
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…
TypeScript
The intrinsic keyword
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
How some utility types are implemented
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…
TypeScript
A note about lib.dom.d.ts
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…
TypeScript
Source Code Walkthrough InlayHints
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…
TypeScript
Arrow Function in Classes
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…
TypeScript
Type Annotation vs Type Assertion
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…
TypeScript
Lookup Types
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…
TypeScript
Enums at runtime
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…
TypeScript
The unknown Type
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…
TypeScript
Implement Rust Result type
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…
TypeScript
Testing with Jest
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…
TypeScript
Build a library with Parcel
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…
TypeScript
Type Intersection
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…
TypeScript
Non-null assertion operator
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…
TypeScript