object type Object Types
Last updated: 2026-10-22
object type Object Types
Object types describe an object via "property: type" pairs: inline them for one-off use, name them with interface or type for reuse. TS judges compatibility by shape.
| Group | Type Composition |
|---|---|
| Kind | Type |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
const user: { id: number; name: string; tags?: string[] } = {
id: 1,
name: "Ada",
};
⚙️ Parameters
| { name: string } | Property declarations separated by ; or , whose value types can be anything (including nested objects) |
|---|---|
| ?/readonly | The optional and readonly modifiers apply to inline object types too |
Returns:None — object types are type-level only and produce no runtime code.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
5 no label
(Runtime output after transpilation; adding a z field absent from the type triggers the excess property check)
💡 Notes
ℹ️ Info TS is structurally typed: matching shape means compatible — no explicit inheritance required (duck typing).
💡 Tip Once a shape is referenced twice, promote it to an interface/type: shorter errors and single-point edits.
⚠️ Note Object literals trigger the excess property check: unknown fields error immediately; assigning through an intermediate variable bypasses it.
🏷️ Related Type Composition
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q What is structural typing (duck typing)?
A Compatibility depends only on having the required properties with right types, not on where they were declared. {x: 1, y: 2, z: 3} fits {x: number; y: number} just fine.
Q Inline object type or interface?
A Inline for simple one-off shapes; use interface/type for reuse, extension and named IDE hints.
Q How do I annotate object parameters?
A Write the shape inline: function draw(p: { x: number; y: number }) {}. For complex shapes define an interface first.