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.
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.
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!
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.
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With