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.

GroupType Composition
KindType
Strict modeNot 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)
?/readonlyThe 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.
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏