-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Spread Operator Doesn't Keep Object Type #46128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Current behavior is more in line with what actually happens because class WithPrivate {
#hidden: string = 'hidden';
visible: number = 1;
}
const withPrivate = new WithPrivate();
const noPrivate = { ...withPrivate };
const noPrivateError: WithPrivate = { ...withPrivate };
type SetterOnly= {
set prop(val: string);
regular: number;
}
declare const setterOnly: SetterOnly;
const noSetter = { ...setterOnly };
const noSetterError: SetterOnly = { ...setterOnly }; |
I see - thank you for your explanation. In the examples given, doesn't that mean
In your example the class WithPrivate {
#hidden: string = 'hidden';
visible: number = 1;
}
const withPrivate = new WithPrivate();
const noPrivateError:WithPrivate = { ...withPrivate };
const noPrivateAssign:WithPrivate = Object.assign({}, withPrivate);
type SetterOnly = {
set prop(val: string);
regular: number;
}
const setterOnly:SetterOnly = {
regular: 0,
prop: ''
};
const noSetterError = { ...setterOnly };
noSetterError.prop = '3';
const noSetterAssign = Object.assign({}, setterOnly);
noSetterAssign.prop = '2'; |
@GeoMarkou The issue with |
Linking to a similar comment: #36554 (comment) |
Bug Report
π Search Terms
spread operator interface object assign type
π Version & Regression Information
Always
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Using the spread operator to clone an object doesn't retain the object type. This isn't a huge problem, but it makes the Intellisense harder to read, and annoys me. I also feel like it would use more memory / CPU validating another object type instead of just referencing the original.
π Expected behavior
Using the spread operator to clone the object should keep the same type.
The text was updated successfully, but these errors were encountered: