I have a simple model class
export interface Category {
name: string;
description: string;
}
I need to declare and initialize a variable in an angular component. Tried:
category: Category = {};
Error: {} is not assignable to a Category
category: Category = new Category();
error: Category refers to a type, but being referred as value..
Any suggestions?
There are a number of ways to solve this problem, depending on your desired result.
interface
to a class
export class Category {
name: string;
description: string;
}
const category: Category = new Category();
interface
as a class
export class CategoryObject implements Category {
}
const category: Category = new CategoryObject();
interface
const category: Category = {
name: 'My Category',
description: 'My Description',
};
export interface Category {
name?: string;
description?: string;
}
const category: Category = {};
Partial<T>
export interface Category {
name: string;
description: string;
}
const category: Partial<Category> = {};