<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Programming on Commentary of Takao</title><link>https://takao.blog/en/tags/programming/</link><description>Recent content in Programming on Commentary of Takao</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>Commentary of Takao</copyright><lastBuildDate>Sat, 13 Jun 2026 23:11:50 +0900</lastBuildDate><atom:link href="https://takao.blog/en/tags/programming/index.xml" rel="self" type="application/rss+xml"/><item><title>Strict Type Guarding using TypeScript's NoInfer Utility</title><link>https://takao.blog/en/web/typescript-unmapped-types-utility/</link><pubDate>Wed, 10 Jun 2026 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-unmapped-types-utility/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Strict Type Guarding using TypeScript's NoInfer Utility" /&gt;&lt;h2 id="the-widening-problem"&gt;The Widening Problem
&lt;/h2&gt;&lt;p&gt;When TypeScript infers types from multiple call-site arguments, it often &lt;strong&gt;widens&lt;/strong&gt; the inferred type to accommodate all candidates. This is usually helpful, but in certain generic scenarios it produces types that are too loose, letting invalid states slip through.&lt;/p&gt;
&lt;p&gt;Consider a function that accepts a default value alongside a specific key:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;function&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;createState&lt;/span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;T&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;extends&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color:#a6e22e"&gt;key&lt;/span&gt;: &lt;span style="color:#66d9ef"&gt;T&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;defaultValue&lt;/span&gt;: &lt;span style="color:#66d9ef"&gt;T&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; { &lt;span style="color:#a6e22e"&gt;key&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;defaultValue&lt;/span&gt; };
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;state&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;createState&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;color&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;blue&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// T inferred as &amp;#34;color&amp;#34; | &amp;#34;blue&amp;#34; — not what we want
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;TypeScript infers &lt;code&gt;T&lt;/code&gt; as &lt;code&gt;&amp;quot;color&amp;quot; | &amp;quot;blue&amp;quot;&lt;/code&gt; instead of constraining &lt;code&gt;defaultValue&lt;/code&gt; to match &lt;code&gt;key&lt;/code&gt;. The result type loses precision for both parameters.&lt;/p&gt;</description></item><item><title>Building Complex string Parsers inside TypeScript Type System</title><link>https://takao.blog/en/web/typescript-template-literal-type-level-parsers/</link><pubDate>Fri, 10 Apr 2026 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-template-literal-type-level-parsers/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Building Complex string Parsers inside TypeScript Type System" /&gt;&lt;h2 id="parsing-at-the-type-level"&gt;Parsing at the Type Level
&lt;/h2&gt;&lt;p&gt;TypeScript&amp;rsquo;s template literal types, combined with recursive conditional types and &lt;code&gt;infer&lt;/code&gt;, allow you to parse and transform string literals entirely within the type system. This is not a runtime parser—it is a compile-time parser that extracts structure from string constants, enabling strongly typed APIs for URL routing, SQL queries, command-line arguments, and more.&lt;/p&gt;
&lt;h2 id="template-literal-types-primer"&gt;Template Literal Types Primer
&lt;/h2&gt;&lt;p&gt;Template literal types can interpolate other types and infer parts of a string.&lt;/p&gt;</description></item><item><title>Branching in TS Type System: Intro to Conditional Types</title><link>https://takao.blog/en/web/typescript-conditional-types/</link><pubDate>Thu, 15 Jan 2026 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-conditional-types/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Branching in TS Type System: Intro to Conditional Types" /&gt;&lt;h2 id="introduction"&gt;Introduction
&lt;/h2&gt;&lt;p&gt;TypeScript&amp;rsquo;s type system is &lt;strong&gt;Turing-complete&lt;/strong&gt;, and one of the key features that unlocks its full power is &lt;strong&gt;conditional types&lt;/strong&gt;. Just like ternary operators in JavaScript (&lt;code&gt;condition ? a : b&lt;/code&gt;), conditional types let you create type-level logic: &lt;code&gt;T extends U ? X : Y&lt;/code&gt;. This enables dynamic, context-aware type resolution.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="basic-syntax"&gt;Basic Syntax
&lt;/h2&gt;&lt;p&gt;A conditional type checks whether a type &lt;strong&gt;extends&lt;/strong&gt; another type:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;IsString&lt;/span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;T&lt;/span&gt;&amp;gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;T&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;extends&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt; &lt;span style="color:#f92672"&gt;?&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt; &lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;false&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;A&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;IsString&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt;; &lt;span style="color:#75715e"&gt;// true
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;B&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;IsString&lt;/span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;42&lt;/span&gt;&amp;gt;; &lt;span style="color:#75715e"&gt;// false
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The condition &lt;code&gt;T extends U&lt;/code&gt; asks: &amp;ldquo;Is &lt;code&gt;T&lt;/code&gt; assignable to &lt;code&gt;U&lt;/code&gt;?&amp;rdquo; If yes, resolve to the true branch; otherwise, the false branch.&lt;/p&gt;</description></item><item><title>Transforming Types dynamically using Mapped Types</title><link>https://takao.blog/en/web/typescript-mapped-types/</link><pubDate>Mon, 10 Nov 2025 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-mapped-types/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Transforming Types dynamically using Mapped Types" /&gt;&lt;h2 id="introduction"&gt;Introduction
&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Mapped types&lt;/strong&gt; let you transform an existing object type by iterating over its keys. Think of them as &lt;code&gt;Array.prototype.map()&lt;/code&gt; but at the &lt;strong&gt;type level&lt;/strong&gt;. The syntax &lt;code&gt;{ [K in keyof T]: T[K] }&lt;/code&gt; creates a new type by applying a transformation to each property of &lt;code&gt;T&lt;/code&gt;. This is how many of TypeScript&amp;rsquo;s built-in utility types work under the hood.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="basic-mapped-type"&gt;Basic Mapped Type
&lt;/h2&gt;&lt;p&gt;The simplest mapped type creates a clone of an existing type:&lt;/p&gt;</description></item><item><title>Type-Safe State Transitions using Discriminated Unions</title><link>https://takao.blog/en/web/typescript-discriminated-unions/</link><pubDate>Wed, 10 Sep 2025 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-discriminated-unions/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Type-Safe State Transitions using Discriminated Unions" /&gt;&lt;p&gt;In modern frontend and backend development with TypeScript, ensuring type safety during state transitions is a crucial element in building robust and crash-free applications. When handling asynchronous API communications (such as transitioning between loading, success, and error states) or complex user interactions, ambiguous type definitions can easily lead to unexpected runtime errors.&lt;/p&gt;
&lt;p&gt;In this article, we will explore how to harness the power of &lt;strong&gt;Discriminated Unions&lt;/strong&gt; (also known as Tagged Unions) in TypeScript to manage state safely, elegantly, and with minimum boilerplate.&lt;/p&gt;</description></item><item><title>Creating Powerful String Types with TS Template Literals</title><link>https://takao.blog/en/web/typescript-template-literal-types/</link><pubDate>Tue, 10 Jun 2025 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-template-literal-types/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Creating Powerful String Types with TS Template Literals" /&gt;&lt;h2 id="introduction"&gt;Introduction
&lt;/h2&gt;&lt;p&gt;TypeScript offers an advanced type system that allows developers to model complex runtime constraints at compile time.&lt;/p&gt;
&lt;p&gt;Among these features, &lt;strong&gt;Template Literal Types&lt;/strong&gt; (introduced in TypeScript 4.1) allow you to define structured string constraints by combining string literals.&lt;/p&gt;
&lt;p&gt;By applying JavaScript&amp;rsquo;s template literal syntax (&lt;code&gt;`hello ${name}`&lt;/code&gt;) to type space, you can enforce safe CSS class name patterns, automate API route mappings, and construct typed design systems. This guide reviews the core patterns of Template Literal Types.&lt;/p&gt;</description></item><item><title>Mastering the TypeScript satisfies Operator</title><link>https://takao.blog/en/web/typescript-satisfies-operator/</link><pubDate>Mon, 10 Mar 2025 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-satisfies-operator/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Mastering the TypeScript satisfies Operator" /&gt;&lt;h2 id="introduction"&gt;Introduction
&lt;/h2&gt;&lt;p&gt;Introduced in TypeScript 4.9, the &lt;strong&gt;&lt;code&gt;satisfies&lt;/code&gt; operator&lt;/strong&gt; is a powerful feature designed to improve type safety while preserving developer flexibility.&lt;/p&gt;
&lt;p&gt;However, distinguishing the difference between &lt;code&gt;satisfies&lt;/code&gt;, type annotations (&lt;code&gt;: Type&lt;/code&gt;), and type assertions (&lt;code&gt;as Type&lt;/code&gt;) can be confusing. This guide breaks down the mechanics of the &lt;code&gt;satisfies&lt;/code&gt; operator and shares practical use cases for production codebases.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-the-limitation-of-standard-type-annotations-"&gt;1. The Limitation of Standard Type Annotations (:)
&lt;/h2&gt;&lt;p&gt;Let&amp;rsquo;s review the limitations of standard type annotations.&lt;/p&gt;</description></item><item><title>Patterns to Avoid with JavaScript Async/Await</title><link>https://takao.blog/en/web/javascript-async-await-tips/</link><pubDate>Sat, 15 Feb 2025 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/javascript-async-await-tips/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Patterns to Avoid with JavaScript Async/Await" /&gt;&lt;h2 id="introduction"&gt;Introduction
&lt;/h2&gt;&lt;p&gt;Since the introduction of &lt;code&gt;async/await&lt;/code&gt; in ES2017, writing asynchronous JavaScript has become far more intuitive. It allows developers to express asynchronous logic in a synchronous-looking structure, drastically improving codebase readability compared to nested Promise chains (&lt;code&gt;.then().catch()&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;However, this clean synchronous-like syntax can hide performance pitfalls and error-handling bugs. In this article, we&amp;rsquo;ll dive into common async/await antipatterns and learn how to refactor them into high-performance, resilient code.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="antipattern-1-unnecessary-serialization-sequential-awaiting"&gt;Antipattern 1: Unnecessary Serialization (Sequential Awaiting)
&lt;/h2&gt;&lt;p&gt;The most common mistake is executing independent asynchronous tasks sequentially, missing opportunities for parallel execution.&lt;/p&gt;</description></item><item><title>TypeScript Generic Constraints: Building Type-Safe APIs</title><link>https://takao.blog/en/web/typescript-generic-constraints/</link><pubDate>Tue, 05 Mar 2024 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-generic-constraints/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post TypeScript Generic Constraints: Building Type-Safe APIs" /&gt;&lt;p&gt;TypeScript generics let you write reusable, type-safe code, but without constraints they are too permissive. The &lt;code&gt;extends&lt;/code&gt; keyword restricts type parameters to shapes that have specific properties, giving you both flexibility and safety.&lt;/p&gt;
&lt;h2 id="the-extends-constraint"&gt;The extends Constraint
&lt;/h2&gt;&lt;p&gt;The foundation of generic constraints is the &lt;code&gt;extends&lt;/code&gt; keyword. It ensures a type parameter satisfies a required structure:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-gdscript3" data-lang="gdscript3"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;function getProperty&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;T, K &lt;span style="color:#66d9ef"&gt;extends&lt;/span&gt; keyof T&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt;(obj: T, key: K): T[K] {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; obj[key];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here &lt;code&gt;K extends keyof T&lt;/code&gt; guarantees &lt;code&gt;key&lt;/code&gt; is a valid property of &lt;code&gt;obj&lt;/code&gt;. Attempting to pass an invalid key triggers a compile-time error. Common patterns include constraining to interfaces (&lt;code&gt;T extends { id: string }&lt;/code&gt;), union members, or primitive types.&lt;/p&gt;</description></item><item><title>TypeScript Utility Types: Complete Guide with Real Examples</title><link>https://takao.blog/en/web/typescript-utility-types/</link><pubDate>Sun, 10 Dec 2023 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/typescript-utility-types/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post TypeScript Utility Types: Complete Guide with Real Examples" /&gt;&lt;p&gt;TypeScript provides a rich set of built-in utility types that transform and manipulate other types. Understanding these tools lets you write more expressive and type-safe code without reinventing the wheel.&lt;/p&gt;
&lt;h2 id="property-modification-types"&gt;Property Modification Types
&lt;/h2&gt;&lt;p&gt;These types modify the optionality, mutability, and requirement of object properties:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;interface User {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; id: number;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; name: string;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; email?: string;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;type PartialUser = Partial&amp;lt;User&amp;gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;// { id?: number; name?: string; email?: string; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;type RequiredUser = Required&amp;lt;User&amp;gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;// { id: number; name: string; email: string; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;type ImmutableUser = Readonly&amp;lt;User&amp;gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;// { readonly id: number; readonly name: string; readonly email?: string; }
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th&gt;Type&lt;/th&gt;
					&lt;th&gt;Effect&lt;/th&gt;
					&lt;th&gt;Common Use&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;Partial&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;All properties optional&lt;/td&gt;
					&lt;td&gt;PATCH request bodies&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;Required&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;All properties required&lt;/td&gt;
					&lt;td&gt;Form submission validation&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;Readonly&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;All properties read-only&lt;/td&gt;
					&lt;td&gt;Configuration objects&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;code&gt;Partial&lt;/code&gt; is especially useful for API update operations where clients should be able to send a subset of fields:&lt;/p&gt;</description></item></channel></rss>