Value-to-type modeling
Derive a reusable object type from a configuration or registry value without duplicating its structure.
TypeScript · Const
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.
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.
type Name = typeof value;
type Keys = keyof Type;
type Value = Type[Key];typeof, keyof, and indexed access supports compact, maintainable derived APIs.keyof describes property names, not property values, so the result is a union of keys.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.
Derive a reusable object type from a configuration or registry value without duplicating its structure.
Restrict APIs to valid property names and infer the corresponding property value type.
Compose reusable transformations for library and application-wide models.
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.
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.
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.
GenericsKeyof Type OperatorTypeof Type OperatorIndexed Access TypesConditional TypesMapped TypesTemplate Literal Types