Advent of TypeScript (1st-12th/25)

Highly appreciate the hardwork done by TypeHero Team. I will highlight the techniques that I have learned/revised so far.

Manipulate object types

Rewrite the type of an object taking generic <T> where K in keyof T can be overwritten with ANY_TYPE_YOU_WANT_TO_OVERWRITE
type OverwriteKey<T, K extends keyof T, NewType> = {
  [P in keyof T]: P extends K ? NewType : T[P];
};

// Usage:
type OriginalType = {
  name: string;
  age: number;
  isValid: boolean;
};

type NewType = OverwriteKey<OriginalType, 'age', string>;
// Result: NewType will have the 'age' property's type overwritten to string.

Conditional Type

A genral use case of using conditional type to filter values
type FilterChildrenBy<T, U> = T extends U ? never : T;

// Test case:
type test_0_actual = FilterChildrenBy<'nice' | 'nice' | 'nice', 'naughty'>;
// test_0_actual resolves to 'nice' since it filters out 'nice' values that match 'naughty'

Template Literal String Types

For checking if a string starts or ends with another string. This type StartsWith checks whether a string T starts with another string U. If it does, it returns true; otherwise, it returns false.

2 ways to iterate the characters:

  1. ${U}${string | 'any_value'} enfroces the constraint that T must start with U followed by any string or a specific string.
  2. Similarly, ${U}${infer _} does the same thing but infer _ means that the string after U is not important. Putting it together as a placeholder that allows TypeScript to infer the type without explicitly using it
type StartsWith<T extends string, U extends string> = T extends `${U}${infer _}`
  ? true
  : false;

type DoesStartWith = StartsWith<'typescript', 'type'>;
// Result: DoesStartWith is true

type EndsWith<T extends string, U extends string> = T extends `${infer _}${U}`
  ? true
  : false;

Reversing a String

Recusive to iterate the string.
type ReverseString<T extends string> = T extends `${infer First}${infer Rest}`
  ? `${ReverseString<Rest>}${First}`
  : '';

type Reversed = ReverseString<'typescript'>;
// Reversed: 'tpircsepyt'