The TypeScript team has unveiled the beta version of TypeScript 5.6, and it’s brimming with features designed to make our lives easier.
Catching Logical Errors Early: Disallowed Nullish and Truthy Checks One of the standout features in TypeScript 5.6 is the stricter handling of nullish and truthy checks. Previously, TypeScript would quietly accept certain logical errors, leading to potential bugs that were hard to catch. Now, the compiler flags expressions that always evaluate to a specific boolean value, such as:
In this blog post, we’ll explore the concept of deep observation in TypeScript and learn how to create a deep observer using proxies. Deep observation allows you to track changes made to an object, including its nested properties, providing a powerful way to monitor and react to modifications in your data structures.
Understanding Deep Observation Deep observation involves monitoring changes made to an object and its nested properties. When a property is modified, whether it’s a top-level property or a deeply nested one, you can capture and respond to those changes. This is particularly useful when working with complex data structures where changes can occur at various levels of nesting.
I’m always looking for fun little coding challenges that are not full projects, and I thought I would do a fun little licence key generator using Typescript.
Step 1: Setting Up the Project
First, create a new directory for your project and navigate into it using your terminal. Then, initialise a new TypeScript project by running the following command:
npm init -y npm install --save-dev typescript Step 2: Configuring TypeScript
Introduction
In data science and machine learning, cosine similarity is a measure that calculates the cosine of the angle between two vectors. This metric reflects the similarity between the two vectors, and it’s used extensively in areas like text analysis, recommendation systems, and more. This post delves into the intricacies of implementing cosine similarity checks using TypeScript.
Understanding Cosine Similarity and Its Applications in AI
Cosine similarity measures two non-zero vectors of an inner product space. It is defined as the cosine of the angle between them, which is calculated using this formula:
Rate limiting is a crucial aspect of building scalable and secure web applications. It helps prevent abuse and ensures the fair usage of resources. In this blog post, we will explore how to implement token bucket rate limiting in a NestJS application with a reset interval of 24 hours.
What is Token Bucket Rate Limiting? Token bucket rate limiting is an algorithm that controls the rate at which a system processes requests. The main idea is that a fixed number of tokens are added to a bucket regularly. When a request arrives, a token is removed from the bucket. If there are no tokens left, the request is rejected.
In this blog post, we’ll create a simple TypeScript module for parsing Vehicle Identification Numbers (VINs). VIN is a unique 17-character code the automotive industry uses to identify individual vehicles.
To get started, we’ll need to install TypeScript and Jest:
npm install typescript jest @types/jest ts-jest Next, configure Jest to work with TypeScript by creating a jest.config.js file:
module.exports = { preset: 'ts-jest', testEnvironment: 'node', }; Now, let’s write the TypeScript code for parsing VINs. Create a file named vinParser.ts:
Today, we’re going to build a license key generator. This flexible generator will allow you to create keys of varying lengths and segment sizes. We’ll also include a validation function to ensure the keys are correct. And, of course, we’ll write Jest test units to ensure everything works as expected. Let’s get started.
Now, there are much easier and more native ways you can create licence keys. Such as the randomUUID method on the Crypto object. Open up your console and type: window.crypto.randomUUID(); and it will spit out a UUID. However, we’ll go a step further in this post and make the format configurable. This allows you to create keys that have segments with different lengths.
In this post, I am going to go over a few TypeScript features that will make your code cleaner and strongly typed. You might already be familiar with some of these features, and some you might not. Admittedly, I am still finding new things to learn in TypeScript, so skip over the items you’re already familiar with.
1. Type Aliases Type aliases help improve code readability by assigning custom names to existing types. They can be particularly useful when dealing with complex types or frequently used throughout your code. By creating a type alias, you can reduce the complexity of your code and make it easier to understand the intent behind a particular type.
TypeScript is a statically-typed superset of JavaScript introduced by Microsoft in 2012. It’s a language that some developers love to hate, but over the years, TypeScript has won over many sceptics, becoming an essential part of many modern JavaScript projects.
You now have developers that once hated TypeScript liking it. Perhaps one of the most known developer advocates to get their TypeScript stance wrong was Eric Elliott, who published an article where he discusses a TypeScript tax. To Eric’s credit, he doesn’t say not to use TypeScript but attempts to argue against its use.
Password generation is something you’re hopefully using a password manager for these days. However, you might not be aware that modern browsers support some great crypto features.
In this quick little tutorial, we will use the Crypto API to create a strong password generator. The code is remarkably simple, and you can adapt this to generate unique values for games and other purposes besides passwords.
/\*\* \* Generates a random password of the specified length. \* \* @param length The length of the password to generate. \* @returns A Promise that resolves to a string containing the generated password. \* @throws An error if the length argument is less than 1. \*/ const generatePassword = async (length: number): Promise => { if (length < 1) { throw new Error('Length must be greater than 0'); } // Create a new Uint8Array with the specified length. const buffer = new Uint8Array(length); // Get the browser's crypto object for generating random numbers. const crypto = window.crypto || (window as any).msCrypto; // For compatibility with IE11. // Generate random values and store them in the buffer. const array = await crypto.getRandomValues(buffer); // Initialize an empty string to hold the generated password. let password = ''; // Define the characters that can be used in the password. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; // Iterate over the array of random values and add characters to the password. for (let i = 0; i < length; i++) { // Use the modulus operator to get a random index in the characters string // and add the corresponding character to the password. password += characters.charAt(array[i] % characters.length); } // Return the generated password. return password; }; As you can see, the getRandomValues method does all of the heavy lifting here for generating our password. We also define what characters are allowed in our password, allowing us to remove ambiguous characters if we wish.