Question
I have defined a TypeScript interface like this:
interface IModal {
content: string;
form: string;
href: string;
$form: JQuery;
$message: JQuery;
$modal: JQuery;
$submits: JQuery;
}
Then I declare a variable like this:
let modal: IModal;
However, when I try to assign a property such as modal.content, I get an error similar to:
Cannot set property 'content' of undefined
Is it valid to use an interface to describe my modal object, and if so, how should I actually create and initialize the object?
Short Answer
By the end of this page, you will understand what a TypeScript interface does, why let modal: IModal; does not create an object, and how to properly initialize an object that matches an interface. You will also see common patterns for object creation, optional properties, and safer ways to avoid undefined errors.
Concept
A TypeScript interface describes the shape of an object. It tells TypeScript what properties an object should have and what types those properties must use.
For example:
interface IModal {
content: string;
form: string;
href: string;
$form: JQuery;
$message: JQuery;
$modal: JQuery;
$submits: JQuery;
}
This does not create an object. It only defines a contract.
When you write:
let modal: IModal;
you are only declaring a variable that is expected to hold an object matching IModal later. At that moment, modal is still undefined.
So if you do this next:
modal.content = "Hello";
JavaScript tries to access the property on , which causes the runtime error.
Mental Model
Think of an interface like an architectural drawing for a house.
- The drawing tells you how many rooms the house should have.
- It does not build the house.
- Declaring
let modal: IModal;is like saying, "I will have a house that follows this plan." - But until the house is actually built, there is nothing to walk into.
So:
interface IModal { ... }= the planlet modal: IModal;= a promise that a matching object will existlet modal: IModal = { ... }= the actual house being built
Syntax and Examples
Basic syntax
1. Define the interface
interface IModal {
content: string;
form: string;
href: string;
$form: JQuery;
$message: JQuery;
$modal: JQuery;
$submits: JQuery;
}
2. Create an object that matches it
let modal: IModal = {
content: "Modal body",
form: "loginForm",
href: "/login",
$form: $("#loginForm"),
$message: $("#message"),
$modal: $("#myModal"),
$submits: $("button[type='submit']")
};
Now modal is a real object, so you can safely access and update its properties:
Step by Step Execution
Consider this code:
interface IModal {
content: string;
form: string;
href: string;
}
let modal: IModal;
modal.content = "Hello";
What happens step by step
Step 1: The interface is declared
interface IModal {
content: string;
form: string;
href: string;
}
TypeScript learns that any IModal object must have three string properties.
Step 2: The variable is declared
let modal: IModal;
This creates a variable named modal, but no object is assigned to it yet.
At runtime, its value is effectively:
Real World Use Cases
Interfaces are commonly used anywhere you want consistent object structure.
UI state objects
A modal, dropdown, or form component often has related fields grouped into one object:
interface ModalState {
isOpen: boolean;
title: string;
content: string;
}
API response shapes
When fetching data from a server, interfaces describe expected fields:
interface User {
id: number;
name: string;
email: string;
}
Function parameters
Interfaces help functions accept structured input safely:
interface LoginData {
username: string;
password: string;
}
function submitLogin(data: LoginData) {
console.(data.);
}
Real Codebase Usage
In real projects, developers rarely leave typed variables uninitialized if they plan to assign properties immediately.
Common patterns
Initialize immediately
This is the simplest and most common pattern:
const modal: IModal = {
content: "",
form: "",
href: "",
$form: $("#form"),
$message: $("#message"),
$modal: $("#modal"),
$submits: $(".submit")
};
Use optional properties for delayed setup
If some values are assigned later:
interface IModal {
content: string;
form: string;
href: string;
$form?: JQuery;
}
This is useful during progressive initialization.
Use a builder or factory function
Useful when object creation logic is repeated:
Common Mistakes
1. Thinking an interface creates an object
Broken example:
interface IModal {
content: string;
}
let modal: IModal;
modal.content = "Hi";
Problem:
modalis declared but not initialized- no real object exists yet
Fix:
let modal: IModal = { content: "" };
2. Declaring required properties but not providing them
Broken example:
interface IModal {
content: string;
form: string;
}
let modal: IModal = {
content: "Hello"
};
Problem:
formis required but missing
Comparisons
| Concept | What it does | Creates a runtime object? | Best use |
|---|---|---|---|
interface | Describes object shape | No | Defining contracts for objects |
type | Describes a type, often similar to an interface | No | Flexible type composition |
| Object literal | Creates an actual object | Yes | Simple object creation |
class | Defines a blueprint for instances with behavior | Yes, when instantiated with new | Objects with methods and reusable behavior |
Interface vs class
{
: ;
}
{
: ;
() {
. = ;
}
}
modal = ();
Cheat Sheet
interface IModal {
content: string;
form: string;
href: string;
$form: JQuery;
$message: JQuery;
$modal: JQuery;
$submits: JQuery;
}
Key rule
An interface defines shape only. It does not create an object.
Wrong
let modal: IModal;
modal.content = "Hello"; // runtime error
Right
let modal: IModal = {
content: "",
form: "",
href: "",
$form: $("#form"),
$message: $("#message"),
$modal: $(),
: $()
};
FAQ
Does a TypeScript interface create an object?
No. An interface only describes the required structure of an object.
Why is let modal: IModal; undefined?
Because it declares the variable type but does not assign any actual object to it.
How do I create an object that matches an interface?
Assign an object literal, return one from a function, or create a class instance that matches the interface.
Can I assign properties later instead of all at once?
Yes, but the object itself must exist first. If some properties are not ready yet, make them optional or provide placeholder values.
Should I use an interface for a modal object?
Yes. It is a good use case if you want a clear, typed structure for related modal properties.
Is {} as IModal a good solution?
Usually not for beginners. It bypasses useful safety checks and can hide missing-property bugs.
When should I use a class instead of an interface?
Use a class if you need constructors, methods, or reusable behavior. Use an interface when you only need to describe object shape.
Mini Project
Description
Build a small typed modal configuration object in TypeScript. This project demonstrates the main lesson from the question: an interface defines the expected structure, but you must still create a real object before using its properties. It also shows how to update the object safely after initialization.
Goal
Create and use a modal object that follows an interface without causing undefined property errors.
Requirements
- Define an interface for a modal object with at least
content,form, andhref. - Create a real object that matches the interface.
- Update the object after creation.
- Add a function that prints modal information.
- Avoid any access on an uninitialized variable.
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.
Angular formControl Error with Material Autocomplete: Why It Happens and How to Fix It
Learn why Angular says it cannot bind to formControl and how to fix Reactive Forms setup with Angular Material Autocomplete.