A Partial<T>
lets you pass in partial implementation or an interface
or type
as an argument to a function.
Lets look at a simple example. Suppose you have a function that updates some product data like this:
interface Product {
name: string;
description: string;
}
let currentProduct: Product = {
name: "FS2000",
description: "Hippie Control"
};
function update1(product: Product) {
Object.assign(currentProduct, product);
}
Whenever Typescript sees that you are using the update1
function it wants you to pass in an instance that implements the entire Product interface.
Passing in a Partial
will result in compiler errors.
However if you let the function know that it should be expecting a Partial
like this:
function update2(product: Partial<Product>) {
Object.assign(currentProduct, product);
}
Then no compiler errors are produced and product
can partially implement the interface
or type
.