가을기 Workspace

Typescript로 object 초기화 본문

개발/nodejs

Typescript로 object 초기화

가을기_ 2021. 6. 16. 23:19

Way 1: Convert your interface to a class

export class Category {
  name: string;
  description: string;
}
const category: Category = new Category();

Way 2: Extend your interface as a class

 export class CategoryObject implements Category { }
const category: Category = new CategoryObject();

Way 3: Fully specify your object, matching the interface

const category: Category = {
  name: 'My Category',
  description: 'My Description',
};

 

Way 4: Make the properties optional

export interface Category {
  name?: string;
  description?: string;
}

const category: Category = {};

 

Way 5: Change your variable's type to use Partial<T>

export interface Category {
  name: string;
  description: string;
}

const category: Partial<Category> = {};

'개발 > nodejs' 카테고리의 다른 글

Node.js Philosophy - nodejs의 디자인 철학  (0) 2021.09.09
NestJS mongodb module  (0) 2021.05.09
NestJS module  (0) 2021.05.09
NestJS provider  (0) 2021.05.09
NestJS controller  (0) 2021.05.09
Comments