Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript extend an interface as not required

I have two interfaces;

interface ISuccessResponse {     Success: boolean;     Message: string; } 

and

interface IAppVersion extends ISuccessResponse {     OSVersionStatus: number;     LatestVersion: string; } 

I would like to extend ISuccessResponse interface as Not Required; I can do it as overwrite it but is there an other option?

interface IAppVersion {     OSVersionStatus: number;     LatestVersion: string;     Success?: boolean;     Message?: string; } 

I don't want to do this.

like image 818
engincancan Avatar asked Apr 15 '15 12:04

engincancan


People also ask

Can you extend an interface in TypeScript?

In TypeScript, interfaces can also extend classes, but only in a way that involves inheritance. When an interface extends a class, the interface includes all class members (public and private), but without the class' implementations.

How do I omit with interface TypeScript?

Use the Omit utility type to extend an interface excluding a property, e.g. type WithoutTasks = Omit<Employee, 'tasks'>; . The Omit utility type constructs a new type by picking the properties from the provided type and removing the specified keys. Copied!

How do you extend an interface?

An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

How do I make a field optional in TypeScript?

Use the Partial utility type to make all of the properties in a type optional, e.g. const emp: Partial<Employee> = {}; . The Partial utility type constructs a new type with all properties of the provided type set to optional. Copied!


1 Answers

A bit late, but Typescript 2.1 introduced the Partial<T> type which would allow what you're asking for:

interface ISuccessResponse {     Success: boolean;     Message: string; }  interface IAppVersion extends Partial<ISuccessResponse> {     OSVersionStatus: number;     LatestVersion: string; }  declare const version: IAppVersion; version.Message // Type is string | undefined 
like image 187
Brad Avatar answered Oct 08 '22 18:10

Brad