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 ex...
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: ```typescript...
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: ``...
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 comm...
TypeScript
Understanding keyof typeof
In the ["Use String as Enum Key"](/everyday/04-11-2022-typescript-use-string-as-enum-key) article, we use the **keyof typeof** keywords to create a union of an enum's keys. ```typescript enum Editor {...
TypeScript
String as enum key
In TypeScript, you can define an enum and access its value with a _string-like value_, like this: ```typescript enum Editor {...
TypeScript
Some random notes about Classes
Some notes about some not-so-well-known things about TypeScript Classes. ## Parameter properties In TypeScript, you can define a class's properties in two ways: Define it as a class property in the no...
TypeScript
Compilation Process
This article is inspired by the talk [How the TypeScript compiler compiles](https://www.youtube.com/watch?v=X8k_4tZ16qU), you should really check it out for a more in-depth understanding about the Typ...
TypeScript
The intrinsic keyword
In the [previous post](/everyday/03-30-2022-typescript-how-some-utility-types-are-implemented), we learned how some utility types are implemented. The fact that they're all done at the type level is v...
TypeScript
How some utility types are implemented
TypeScript provides several [utility types](https://www.typescriptlang.org/docs/handbook/utility-types.html) to help us manipulate types easier, like: `Partial<T>`, `Required<T>`, `Pick<T, Keys>`,... ...
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](https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts) file. One interesting thing is, this f...
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 go...
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 tak...
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. ```typescript let a = 10; // typeof a == number...
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: ```typescript...
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 ...
TypeScript
Implement Rust Result type
In Rust, there is a [Result<T, E>](https://doc.rust-lang.org/rust-by-example/std/result.html) type that could return either an `Ok(T)` value if the operation is succeeded, or an `Err(E)` value if the ...
TypeScript
Testing with Jest
To use Jest for testing a TypeScript library, we need to install a couple of things: ``` yarn add -D jest @types/jest ts-jest ts-node...
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 ha...
TypeScript
Type Intersection
Let's say, we are building a component to display some images (with lazy loading): ```javascript const LazyImage = (props: ImageProps) => {...
TypeScript
Non-null assertion operator
Sometimes, you'll run into a case where TS keeps complaining about a possibly undefined value. For example: ```javascript const numToString = (a?: number) => {...
TypeScript