Object property signatures
Concept and syntax
An object type describes named properties and their types. { label: string; count: number } requires both fields. Property signatures use a colon between name and type; a semicolon separates signatures. The object literal itself uses values instead of type names. Use the shape as a contract for functions receiving records so callers supply the required information.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
function describe(item: { label: string; count: number }): string { return item.label + ":" + item.count;}console.log(describe({ label: "tea", count: 3 }));Expected output (one line per log):
tea:3Common mistake
Intentionally invalid; check this snippet separately. Do not run it.
function describe(item: { label: string; count: number }) { return item.label; }describe({ label: "tea" });Expected compiler diagnostic: TS2345. The required count field is missing. Supply a numeric count even if this implementation currently only reads label.
Practice and recap
Make a function accepting label and count, then call it with a complete object and one missing count. Correct the second call.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Object property signatures — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
Which field must exist in { label: string; count: number }?
What is the type of item.count in this contract?
Which object argument meets the contract?
function describe(item: { label: string; count: number }) { return item.label; }describe({ label: "tea" });