Basic syntax

suggest change

Prefer const, use let only for reassignment, and let inference handle obvious local types. Add annotations where they document a public contract. Use satisfies to check a value against a type without replacing the value's inferred type.

const serviceName = "catalog";
let requestCount = 0;

type Status = "idle" | "loading" | "ready";

interface RequestOptions {
  endpoint: string;
  retries?: number;
}

const options = {
  endpoint: "/catalog",
  retries: 3,
} satisfies RequestOptions;

function beginRequest(status: Status): string {
  requestCount += 1;
  return `${serviceName}: ${status} (${requestCount})`;
}

console.log(beginRequest("loading"), options.endpoint);

Prefer narrowing to assertions. When an assertion is unavoidable, value as Type works in both .ts and .tsx, unlike angle-bracket syntax. Assertions are removed during compilation and do not validate values at runtime.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents