Type Script Interface is not working in Ionic 3

Hi Team,

User define Interface is not working in ionic 3.

What I did.
Step 1. created a separate file and export Interface
interface KeyActivityDetails{
title:string;
displayHistoryArrow:boolean;
}

Step 2. imported above interface
Step 3 used as obj :KeyActivityDetails
Step 4. when I am trying to assign a value to it … showing can’t access title of undefined.

Plz help

@Spsahu The interface just indicates the type, it doesnt’t instantiate (create) the object.

If in your some.interface.ts file you have:

export interface KeyActivityDetails {
    title: string;
    displayHistoryArrow: boolean;
}

Use it in your file that uses your interface like the following (for example):

private someMethod() {
	// Here you create the object
	let obj: KeyActivityDetails = { title: 'Title01', displayHistoryArrow: true };

	// Now that the object is created, you can change its title
	obj.title = 'Title02';

	return obj;
}

Or if your object should be an attribute of your class:

private obj: KeyActivityDetails;

private someOtherMethod() {
	if (!this.obj) {
		// Instantiate the object if it is not instantiated yet
		this.obj = ... // get the value from somewhere, or just give it some default value as {}
	}

	this.obj.title = 'Title02';
}
1 Like