#235·ts-reset

Make `dateObject.toISOString().split("T")[0]` return `string` instead of `string | undefined`

Author: ulrichstarkCreated May 6, 2026Updated May 7, 2026

dateObject.toISOString().split("T")[0] is a common pattern to get the date part of an ISO string. Because TypeScript doesn't know that toISOString always returns some string containing a "T", the whole code evaluates to string | undefined but in reality it's always string.

My proposal:

typescript
type IsoDateTimeString = `${string}T${string}`;

declare global {
  interface Date {
    toISOString(): IsoDateTimeString;
  }

  interface String {
    split(this: IsoDateTimeString, separator: "T", limit?: number): [string, string];
  }
}