Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Const

type

TypeScript type operators derive new types from existing types or values. The page introduces generics, keyof, typeof, indexed access, conditional, mapped, and template literal types as tools for concise, maintainable type transformations.

How it works

Type operators let you define types from existing types or values. typeof captures a value's static type, keyof produces its known public property names, and indexed access selects a property type, allowing large codebases to keep derived types synchronized with source declarations.

Syntax

type Name = typeof value;
type Keys = keyof Type;
type Value = Type[Key];

Key points

  • TypeScript can express a type in terms of another type, with generics providing the basic parameterized form.
  • Type operators are part of the language's toolkit for deriving new types instead of restating existing shapes.
  • Value-derived types let declarations stay connected to implementation values.
  • Combining operators such as typeof, keyof, and indexed access supports compact, maintainable derived APIs.

Common mistakes

  • keyof describes property names, not property values, so the result is a union of keys.
  • When deriving keys from index-signature types, only string or number index parameters are supported.

Example: Derive types with typeof, keyof, and indexed access

const person = { name: "Ada", age: 36 };
type Person = typeof person;
type PersonKeys = keyof Person;
type PersonName = Person["name"];

const key: PersonKeys = "name";
const value: PersonName = person[key];
console.log(key, value);

This runnable example derives a type from a value, obtains its property-name union, and indexes that type to obtain the property value type.

When to use it

Value-to-type modeling

Derive a reusable object type from a configuration or registry value without duplicating its structure.

Type-safe property access

Restrict APIs to valid property names and infer the corresponding property value type.

Maintainable type composition

Compose reusable transformations for library and application-wide models.

Interview questions

QWhat does keyof produce?

keyof T is a type operator that produces the union of known, public property names of T. It is useful for constraining a generic key so property access is checked against the source type.

Follow-up: How do you obtain the type stored at a key? Use indexed access, such as T[K], where K is a valid key type.

QHow is typeof used as a type operator?

A type-position typeof creates a type from an existing value, such as type User = typeof user. This avoids duplicating the value's structural type in a separate declaration.

Follow-up: Can it inspect a runtime value dynamically? No; it reflects the value's compile-time type, not runtime data.

QWhat is an indexed access type?

Indexed access types use T[K] syntax to access a subset of a type. If K is a property key, the result is the corresponding property type; if K is a union, the result combines the selected property types.

Follow-up: What constraint is needed for generic K? It should be constrained with K extends keyof T so every requested key is valid.

  • Generics
  • Keyof Type Operator
  • Typeof Type Operator
  • Indexed Access Types
  • Conditional Types
  • Mapped Types
  • Template Literal Types

Source: Documentation - Creating Types from Types. Checked 2026-10-15.