Skip to article content
FrontendXLesson 27 / 40

const type parameters and readonly inference

Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.

advanced · about 45 minutes · 3 exercises

  • Predict
  • Explain
  • Implement

What you will be able to do

#
  • Use a const type parameter to infer literal types.
  • Explain how it differs from as const at the call site.
  • Apply it to config and lookup helpers.

Overview

#

The problem

A generic usually widens literals.

snippet.ts
function tuple<T extends readonly unknown[]>(items: T): T { return items; }
const t = tuple(["a", "b"]); // string[]

const type parameters

snippet.ts
function tuple<const T extends readonly unknown[]>(items: T): T { return items; }
const t = tuple(["a", "b"]); // readonly ["a", "b"]

The const modifier makes the checker infer the narrowest literal types, like as const at the call site, without the caller writing it.

Where it helps

Config objects, route tables and lookup maps where the exact keys and values matter.

snippet.ts
function defineRoutes<const T extends Record<string, `/${string}`>>(routes: T): T {
  return routes;
}

Limits

It only affects inference, not runtime. It can over-narrow when you actually wanted a wide type; in that case annotate the variable instead.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What is the type of `t`? ```ts function id<const T>(value: T): T { return value; } const t = id({ mode: "dark" }); ```

    Choose the best answer
  2. Exercise 2Implement

    Write `defineConfig<const T extends Record<string, string>>(config: T): T` so the returned keys and values stay literal.

    starter.typescript
    export function defineConfig<const T extends Record<string, string>>(config: T): T {
      // your code
    }
    

    Reveal the reference answer
    reference.typescript
    export function defineConfig<const T extends Record<string, string>>(config: T): T {
      return config;
    }
    

    Authored expected output: Compile-time: the returned object keeps literal keys and values. No runtime output.

    The const modifier narrows the inference instead of widening to Record<string, string>.

  3. Exercise 3Explain

    How does a const type parameter differ from asking the caller to write `as const`?

    Reveal the reference answer
    reference.typescript
    A const type parameter moves the narrowing into the function signature, so every caller gets literal inference without writing anything. as const is a call-site assertion the caller must remember to add. The type parameter is more ergonomic and consistent, while as const also affects the runtime value's readonly-ness at that site.

    Authored expected output: A written answer naming inference-site narrowing versus call-site assertion.

    The difference is where the narrowing decision is made.

Common misconceptions

#
  • const type parameters change runtime values.
  • They are the same as a const enum.
  • They always produce the desired type.

Follow-up practice

#
  • typescript-result-pattern

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.