Skip to main content

89 posts tagged with "TypeScript"

The TypeScript programming language.

View All Tags

ts-loader goes webpack 4... again!

· 6 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

Back in 2021 I published a post called ts-loader goes webpack 5; a big exciting post about how ts-loader was upgraded to directly support webpack 5, and drop support for webpack 4 in v9 of ts-loader.

For reasons which I'll get into shortly, as of v9.6.0, ts-loader now supports both webpack 5 (as it did already) and webpack 4. So if you're a webpack 4 user, you can now use ts-loader@9, rather than using ts-loader@8.

title image that reads "ts-loader goes webpack 4... again!" with TypeScript and webpack logos

Only Node.js subpaths with no-restricted-imports and perfectionist/sort-imports

· 5 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

I've recently been experimenting with Node.js subpath imports. My motivation is a general dislike of relative imports. I don't enjoy seeing import { thing } from '../../file.js' style imports in code. By using subpath imports instead I might have import { thing } from 'src/folder/file.js' and that feels much cleaner to me.

But I also like consistency in my codebase, and I don't want to have a mix of import styles. So while using subpath imports can help me avoid relative imports, I also want to make sure that everyone on the team is using the same style. How? Here's how!

title image reading "Only Node.js subpaths with no-restricted-imports and perfectionist/sort-imports"

Full-stack static typing with OpenAPI TypeScript and Microsoft.AspNetCore.OpenApi

· 13 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

I've long believed in the benefits of static typing. Static typing helps you catch errors early, improves code navigation and makes refactoring easier. In recent years I've been using TypeScript on the front end and C# on the back end to get these benefits. I wrote previously about how to do this with NSwag and I thought it was probably worth returning to the topic. How would I do the same thing now?

Yargs: statically typed builder commands

· 5 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

Yargs is a popular library for building command line interfaces in Node.js. And the name is just fabulous. Yargs provides a way to define commands, options, and arguments in a structured way. However, Yargs has been around for a long time and it the documentation makes little mention of TypeScript support.

title image reading "Yargs: statically typed builder commands" with the Yargs and TypeScript logos

Whilst there is some documentation, if you're building more involved command line interfaces with Yargs in TypeScript, you may find that you need to do a bit of extra work to get strong typing working well with commands that have builders. In this post, I'll demonstrate how to use Yargs to create statically typed commands with builders in TypeScript.

Before we start, I should say that I'm working with Yargs version 18.0.0 in this post. The type definitions come from Definitely Typed and the version is 17.0.35. However, there is no significant difference in the types between Yargs 17 and 18 and so the difference is not an issue.

As an aside, it's possibly worth mentioning that these days it's possible to go without third party libraries entirely to parse command line arguments thanks to features like parseArgs which have been part of Node.js since version 18. However, Yargs remains a popular choice and is still widely used. I have no plans to replace Yargs in my existing projects just yet.

Keeping front end and back end in sync with NSwag generated clients

· 6 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

For many years I've been a big fan of using NSwag to generate TypeScript and CSharp clients for APIs. I've written about it before in Generate TypeScript and CSharp clients with NSwag.

title image reading "Keeping front end and back end in sync with NSwag generated clients" with the Open API, TypeScript and C# logos

You're likely aware of the popularity of excellent projects like tRPC which provide a way to use TypeScript end-to-end. However, if you're working in a polyglot environment where your back end is written in C# or [insert other language here], and your front end is written in TypeScript, then cannot take advantage of that. However, by generating front end clients from a server's OpenAPI specs, it's possible to have integration tests that check your front end and your back end are aligned.

This post will show you how to do that using NSwag.

Azure DevOps: merging pull requests with conventional commits

· 9 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

There was a time in my life when I didn't really care about commit messages. I would just write whatever I felt like, and it was fine. Over time, I learned that good commit messages are important for understanding the history of a project, especially when working in a team. And also, because I tend to forget what I've been working on surprisingly quickly.

There's also more technical reasons to care about commit messages. For example, if you're using a tool like semantic-release to automate your release process, it relies on conventional commit messages to determine the next version number and generate release notes. It turns out that Azure DevOps has some challenges when it comes to maintaining a git commit history of conventional commits, especially when merging pull requests. By default, Azure DevOps uses a commit strategy that creates a merge commit with a message like "Merge PR 123: Title of pull request". This is acts against conventional commits.

title image reading "Azure DevOps: merging pull requests with conventional commits" with an Azure DevOps logo

You can use the UI to change the commit message when completing a pull request, but it's very easy to forget to do this. And if you're using squash merges, you lose the individual commit messages from the feature branch, which can be a problem if you're trying to maintain a history of conventional commits.

There is a way to bend Azure DevOps to our will; to allow us to control our commit messages. In this post, I'll show you how to do just that using the Azure DevOps API, some TypeScript and build validations. The fact this mechanism lives in a build validation means you cannot forget to set the commit message. That's the feature.

This post is not, in fact, specifically about using conventional commits. That's just a common use case. Rather this post is about being able to control the commit message when merging pull requests in Azure DevOps.

Azure DevOps: merging pull requests and setting autocomplete with the API

· 4 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

Have you ever wanted to merge a pull request in Azure DevOps using the Azure DevOps API? Or set a pull request to autocomplete, so it automatically merges when all policies are satisfied? If so, you're in the right place. In this post, I'll show you how to do just that using the Azure DevOps Client for Node.js.

title image reading "Azure DevOps: merging pull requests and setting autocomplete with the API" with an Azure DevOps logo

I'm using the Azure DevOps Client for Node.js; but if you want to use the REST API directly, you can do that too. The principles are the same, but you'll need to make HTTP requests instead of using the client library.

To get up and running with the Azure DevOps Client for Node.js, you can see how we work with it in this post on dynamic required reviewers in Azure DevOps post. This will help you set up your environment and authenticate with Azure DevOps.

If you'd like to read about setting commit messages when merging pull requests in Azure DevOps, you can check out my post on merging pull requests with conventional commits in Azure DevOps.

Azure DevOps: using DefaultAzureCredential in an Azure Pipeline with AzureCLI@2

· 6 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

I frequently build scripts that work against Azure resources using the Azure SDK for JavaScript. I use the DefaultAzureCredential to authenticate against Azure resources - this is also available in other platforms such as .NET.

title image reading "Azure DevOps: using DefaultAzureCredential in an Azure Pipeline with AzureCLI@2" with an Azure / Azure DevOps logos

The DefaultAzureCredential is a great way to authenticate locally; I can az login and then run my script, safe in the knowledge that the DefaultAzureCredential will authenticate successfully. However, how can I use the DefaultAzureCredential in an Azure DevOps pipeline?

This post will show you how to use the DefaultAzureCredential in an Azure DevOps pipeline, specifically by using the AzureCLI@2 task.

Azure DevOps: pull requests and dynamic required reviewers

· 11 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

Have you ever wanted to have required reviewers for a pull request in Azure DevOps? Probably. And that's an inbuilt feature of Azure DevOps. By using branch policies, you can set required reviewers for a pull request. If you want to ensure the code is reviewed by the appropriate people before it is merged into the main branch, this can prove very useful.

title image reading "Azure DevOps: pull requests and dynamic required reviewers" with an Azure DevOps logo

However, the required reviewers are static. You can set them up in the branch policies, but they don't change dynamically based on the code being altered or the people involved in the pull request. I spent many moons trawling the internet for an answer to this question, and I found that many people were asking the same question. The answer was always the same: "You can't do that."

However, there is a way. It is, hand on heart, marginally clunky. But the clunk is marginal, and more than acceptable. It involves co-opting build validations to achieve the desired effect. In this post, I'll show you how to do that.

TypeScript is going Go: Why it’s the pragmatic choice

· 16 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET
Ashley Claymore
Bloomberg. TC39.

TypeScript is being ported to Go. This is known as "TypeScript 7" (it is currently on 5.8). It's quite likely that you know this by now, as there have been excellent communications from the TypeScript team in a variety of forums. In fact, hats off to the team; it's been an object lesson in how to communicate well; straightforward, clear and open.

title image showing TypeScript and Go logos

There's no shortage of content out there detailing what is known about the port. This piece is not that. Rather, it's the reflections of two people in the TypeScript community. What our thoughts, feelings and reflections on the port are.

It's going to be a somewhat unstructured wander through our reactions and hopes. Buckle up for opinions and feelings.

Microsoft Graph client: how to filter by endswith

· 8 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

In this post we're going to look at filtering using an endswith filter with the Microsoft Graph client. This falls into the category of "Advanced query capabilities on Microsoft Entra ID objects" and I found tricky to get working.

title image reading "Microsoft Graph client: how to filter by endswith" with the relevant logos

Performing an endsWith or similar filter shouldn't be difficult. But the method of how to do so isn't obvious. If you've ever encountered a message like this:

Operator 'endsWith' is not supported because the 'ConsistencyLevel:eventual' header is missing. Refer to https://aka.ms/graph-docs/advanced-queries for more information

Then this blog post is for you.

List Pipelines with the Azure DevOps API

· 3 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

Listing the Azure Pipelines using the Azure DevOps REST API and TypeScript is possible, but if you use the official Azure DevOps Client for Node.js, you might have issues. This is because it does not support pagination. So if you have a project with a large number of pipelines, then using the official client might mean you cannot retrieve it.

title image reading "List Pipelines with the Azure DevOps API" with the relevant logos

This post implements an alternative mechanism, directly using the Azure DevOps API and thus handling pagination. If you're curious as to how to create a pipeline, then check out my post on creating a pipeline with the Azure DevOps API.

Node.js, Azure Application Insights, and Fastify

· 5 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

If you deploy a Node.js application to Azure, you might want to use Azure Application Insights to monitor it. This post shows you how to set up a Node.js application with Azure Application Insights. It also includes a Fastify plugin to automatically track requests. (Given the out of the box mechanism for tracking requests does not work with Fastify.)

title image reading "Node.js, Azure Application Insights, and Fastify" with the relevant logos

This is one of those posts that gathers together information I found doing research and puts it in one place.

Get Service Connections with the Azure DevOps API (REST and TypeScript)

· 5 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

If you work with Azure Pipelines, you'll likely have come upon the need to create service connections. These are the connections to external services that your pipelines need to run. You can interrogate these connections using the Azure DevOps REST API. This post goes through how to do this; both using curl and using TypeScript.

title image reading "Get Service Connections with the Azure DevOps API" with the Azure DevOps logo

I'm writing this post because when I attempted to use the Azure DevOps Client for Node.js package to acquire them I found it lacking, and not for the first time. I am going to allow myself a little moan here; ever since Microsoft acquired GitHub, the Azure DevOps ecosystem feels like it has had insufficient investment.

However, as is often the case, there is a way. The Azure DevOps REST API is there for us, and with a little fetch we can get the job done.

Azure DevOps API: Set User Story column with the Azure DevOps Client for Node.js

· 5 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

When I attempted to set the column of a User Story in Azure DevOps using the Azure DevOps Client for Node.js, I was surprised to find that the field System.BoardColumn was read-only and I bumped into the error:

TF401326: Invalid field status 'ReadOnly' for field 'System.BoardColumn'.

title image reading "Azure DevOps API: Set User Story column with the Azure DevOps Client for Node.js" with an Azure DevOps logo

This post explains how to set the column of a User Story in Azure DevOps using the Azure DevOps Client for Node.js and it's based in part on a Stack Overflow question.

Static Typing for MUI React Data Grid Columns

· 6 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

The MUI X Data Grid is a really handy component for rendering tabular data in React applications. But one thing that is not immediately obvious is how to use TypeScript to ensure that the columns you pass to the component are correct. This post will show you how to do that.

title image reading "Static Typing for MUI React Data Grid Columns" with a TypeScript logo and MUI logos

Why does it matter? Well look at this screenshot of the Data Grid with incorrect column names:

screenshot of a grid with incorrect columns

typescript-eslint with JSDoc JavaScript

· 12 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

It's possible to statically type check a JavaScript codebase with TypeScript with JSDoc annotations. Going a little further, and using typescript-eslint to lint your codebase with the benefits of type information can improve your code quality even more. This post will show you how to set this up and talk about some of the gotchas.

title image reading "typescript-eslint with JSDoc JavaScript" with a typescript-eslint logo and TypeScript logo

We'll also talk a little about how to use TypeScript in combination with JSDoc annotations in a JavaScript codebase.

Web Workers, Comlink, Vite and TanStack Query

· 6 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

I've written previously about combining Web Workers and Comlink. I recently found myself needing to use Web Workers again. As I picked them up this time I found myself making some different choices, now I was working in a codebase that used Vite to build. I ended up using TanStack Query in combination with my Web Workers and Comlink. It was a really good fit and this post will show you how to use Web Workers with Comlink and TanStack Query.

title image reading "Web Workers, Comlink, Vite and TanStack Query" with logos

ESLint no-unused-vars: _ ignore prefix

· 6 min read
John Reilly
OSS Engineer - TypeScript, Azure, React, Node.js, .NET

I'm a longtime user of TypeScripts noUnusedLocals and noUnusedParameters settings. I like to avoid leaving unused variables in my code; these compiler options help me do that.

I use ESLint alongside TypeScript. The no-unused-vars rule provides similar functionality to TypeScripts noUnusedLocals and noUnusedParameters settings, but has more power and more flexibility. For instance, no-unused-vars can catch unused error variables; TypeScript's noUnusedLocals and noUnusedParameters cannot.

One thing that I missed when switching to the ESLint option is that, with noUnusedLocals and noUnusedParameters, you can simply ignore unused variables by prefixing a variable with the _ character. That's right, sometimes I want to declare a variable that I know I'm not going to use, and I want to do that without getting shouted at by the linter.

It turns out you can get ESLint to respect the TypeScript default of ignoring variables prefixed with _; it's just not the default configuration for no-unused-vars. But with a little configuration we can have it. This post is a quick guide to how to implement that configuration.

title image reading "From TypeScript noUnusedLocals and noUnusedParameters to ESLint no-unused-vars (with _ prefix)" with the ESLint and TypeScript logo