Partial
Partial<>
Type
(important) Partial type makes all fields optional on a type. If you have an identical type that requires any of the fields, two types will not be equal to each other in regards to required field
What is it
Constructs a type with all properties of passed type set to optional
- Because all the fields become optional, the new type is considered a
subset
of the passed type
Using partial
will fulfill the type expectation with select fields (hence, the subset)
interface Todo { title: string; description: string; } function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) { return { ...todo, ...fieldsToUpdate }; } // full definition const fullTodo = { title: "organize desk", description: "clear clutter", }; // partial definition, leaving out `title` property const partialTodo = updateTodo(todo1, { description: "throw out trash", });
Use cases
- using it as a temporary measure on
unknown
data type - not trusting the query to return everything at once
- e.g. not using Prisma type alone, but external type reference
When using a partial, you need a ?
on an optional field that is not required in implementation
partialTypedData?.optionalField