-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Class.assign() to return an typed Object.assign() #11508
Comments
Take a look at lib.es6.d.ts - it defines
So the returned type will be the intersection of the input types. This is usually a good enough approximation to the type of the resulting object (things get weird if you try to merge a function onto another function). |
I think what I'm proposing would be like this assign<T>(original: T, ...sources: any[]): T; or assign<T>(target: {}, original: T, ...sources: any[]): T; or assign<T>(target: any, ...sources: any[]): T; So the returned type would be the original type to assign. Right? The behavior that I would like it's giving a |
This is a duplicate of #11100. @michaeljota, i do not think the proposed declaration matches the semantics of Object.assing. a more accurate declaration would use the spread operator as such: assign<T, U>(target: T, source: U): { T, ...U }; |
closing in favor of #11100. |
I read #11100. I know that plain in JS the desired behavior would be to copy all properties, but in TypeScript, would nice to have something that instead of giving us a intersection of all the types in the assignment, someone could explicitly ask for a giving type. I wasn't mainly asking for I don't know. I see this like an alternative. Anyway, thanks. :). |
You can always assert the generics directly and then it will guard against them: interface Foo {
foo: string;
}
interface Bar {
bar: string;
}
const foobar1 = Object.assign({ foo: 'bar' }, { bar: 2 }); // No error
const foobar2 = Object.assign<Foo, Bar>({ foo: 'bar' }, { bar: 2 }); // Type 'number' is not assignable to type 'string'. |
nothing stops you from changing the declaration of assign locally, or adding a new wrapper for Object.assing that have a different declaration. but I believe the declaration in lib.d.ts should model the runtime behavior as much as possible. |
Yeah. I though about it, you are right. I think that what I'm looking changes completely the runtime behavior, and would need additional twerks to make it work. Thank you. |
Scenario:
Right now Object.assign() doesn't return a typed Object. And as I see it, it should not return one. The Object.assign() should be use as in plain JS, allowing to shallow merge any typed objects.
Instead, I would suggest the use of a .assign(). This assign() should be a method in the Class object, and not in the instance. This would allow to explicit request and be sure you will only retrieve a typed object with the wanted class, in a way that I find easy to remember and logical to reproduce.
Syntactic:
Emit:
This emit something like this (got it from the playground):
Compatibility:
I just test this in the playground, but this is just plain JS to get a typed object.
Other:
I don't see that this can have any performance issues. I search a little before post this, but I have to recognize, but search was not exhaustive. This can help this #11100, and I think this can help this as well #11150, cause the spread can call it's own Class.assign internally.
I really hope you like this. :). Thanks you for reading.
The text was updated successfully, but these errors were encountered: