-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 3.1.0-dev.20180822
Search Terms: object spread assignability inference equals undefined throw error assignable
Code
interface IModel {
index?: number;
}
function fillOut(model: IModel) {
if (model.index === undefined) {
throw new Error("");
}
const index: number = model.index; // OK
const directModel: Required<IModel> = model;
// ~~~~~~~~~~~
// Type 'IModel' is not assignable to type 'Required<IModel>'.
// Property 'index' is optional in type 'IModel' but required in type 'Required<IModel>'.
const spreadModel: Required<IModel> = {
// ~~~~~~~~~~~
// Type 'IModel' is not assignable to type 'Required<IModel>'.
// Property 'index' is optional in type 'IModel' but required in type 'Required<IModel>'.
...model,
// index: 0,
};
}Expected behavior:
spreadModel should be allowed, as we've inferred that model.index must be of type number, and unlike directModel, there's no allowed way to modify spreadModel after the fact.
Actual behavior:
The type narrowing on model is forgotten in the ... spread.
eamodio and ethanresnick
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created