Back to questions
JuniorTheoryCommon
What does tsc report for this file?
interface Order {
readonly id: number;
address: string;
}
const patch: Partial<Order> = {};
patch.address = "Oak 2";
patch.id = 8;An error: the property id is read-only
- Expecting
Partialto removereadonlytoo: it only adds?, soidstays read-only and the assignment is error TS2540. - Expecting
constto freeze the object: it forbids only a new value for the variable, sopatch.address = "Oak 2"compiles.
- How would you type a patch that cannot carry
idat all? - Does
readonlystop{ ...order, ...patch }from replacingid?