In this lesson you will
- Name the shape of an object with interface or type and use it as a type
- Use optional and readonly properties and arrays of objects
- Extend and combine types with extends and &
- Explain structural typing and choose between interface and type
Naming the shape of an object
An interface lists the properties an object must have and the type of each: interface Product { name: string; price: number; stock: number }. By convention the name starts with a capital letter. Now const dates: Product = { name: "Ajwa dates", price: 45, stock: 12 } is checked property by property. Leave one out and the compiler says it is "missing in type". Misspell one, such as prise, and it says that "Object literal may only specify known properties", often with a "Did you mean" suggestion. Read a property that does not exist and you get TS2339.
The same shape can be written as a type alias: type Product = { name: string; price: number; stock: number };. Note the equals sign and the semicolon at the end. For a plain object shape, the two are interchangeable and the error messages are the same. Once a shape has a name, use it in function parameters, which is where it helps most: function lineTotal(p: Product, qty: number): number. Inside the function your editor suggests name, price and stock as you type, and outside it every call is checked.
Console
- Console messages, errors, form data and clicked links appear here.
Line 11 misspells price as prise. The compiler reports the unknown property and even suggests the right name; once you fix it, the required price is there too. Line 23 reads a property that does not exist. Fix the typo and delete the last log, and the Console reports no type errors.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Should I use interface or type in TypeScript?
For object shapes either works, and the TypeScript Handbook leaves it to you. Use type when you need a union, tuple or other non-object type. Many teams default to interface for objects because error messages name it clearly and it can be extended; consistency matters more than the choice.
Do interfaces exist in the JavaScript output?
No. Interfaces and type aliases are types only, so the compiler removes them completely. You cannot check at run time whether an object "is" an interface; you check its properties instead, which the next lesson shows.
How do I type an object whose keys are not known in advance?
Use Record<string, number>, read "an object with string keys and number values", for example stock counts by product code. An index signature, { [code: string]: number }, means the same. Reading a key may give undefined, so check it.
What is the I prefix in names like IProduct?
It is an older naming habit from C# that some codebases still use for interfaces. The TypeScript team does not use it in its own guidelines, and most modern projects write plain names such as Product.
Where should I put my TypeScript types in a project?
Put types used by one file next to the code that uses them, and shared shapes, such as the data an API returns, in a shared file like types.ts that other files import with import type { Product } from "./types".
Sources
Facts in this lesson were checked against these official pages on 17 November 2026.
- TypeScript Handbook: Object Types
- TypeScript Handbook: Everyday Types (interfaces and type aliases)
- TypeScript Handbook: Type Compatibility
- TypeScript Handbook: Utility Types (Record)
- TypeScript Handbook: Declaration Merging
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.