Is it possible to automatically derive this interface:
interface OverrideParamType {
foo?: FooType
bar?: BarType
}
from this one
interface ParamType {
foo: FooType
bar: BarType
}
The use is in functions ending with:
return Object.assign ( {}, baseParams, overrideParams )
In the TypeScript, Partial<T> is implemented as below; it makes properties optional. type Partial<T> = { [P in keyof T]?: T[P]; }; In summary, whenever you need to make all properties optional, use the Partial<T> type.
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.
To make a single property in a type optional, create a utility type that takes a type and the property name as parameters and constructs a new type with the specific property marked as optional.
The declaration of the interface is a place to make some properties optional. You can achieve that with a question mark next to the properties names. In the above example, the pet is an optional property.
Since typescript 2.1 you can do:
interface ParamType {
foo: FooType
bar: BarType
}
type PartialParamType = Partial<ParamType>;
The definition of Partial
is:
type Partial<T> = {
[P in keyof T]?: T[P];
};
More on that in: Mapped Types
An example in playground.
Note that there's no need to define the Partial
type yourself, it's part of the lib.d.ts.
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