-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Object.Assign needs better alternatives regarding stronger type inference #47130
Comments
@jpike88 in your example Remember objects can implement more than one interface - extra members will never cause a type error. interface MyType {
a: number;
}
const z1 : MyType = Object.assign({a:1}, {c:1}); // No error - a is present and has correct type.
const z2 : MyType = Object.assign({b:1}, {a:1}); // No error - a is present and has correct type.
const z3 : MyType = Object.assign({b:1}, {z:1}); // error - a is missing.
//Property 'a' is missing in type '{ b: number; } & { z: number; }' but required in type 'MyType'. ts(2741)
const z3 : MyType = Object.assign({b:1}, {a:false}); // error - a has the wrong type
//Type '{ b: number; } & { a: boolean; }' is not assignable to type 'MyType'.
// Types of property 'a' are incompatible.
// Type 'boolean' is not assignable to type 'number'. ts(2322) |
Excess property checks are purely a lint-level check and aren't part of the type system proper. You can always have more properties than the type says, because that's how subtyping works in the structural type system. You're probably looking for Exact Types: #12936 |
Yes it's Exact Types by the looks of it... a proposal over 5 years old so I'm not going to hold my breath lol |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
I have looked at other discussions somewhat related to this, offering weird tricks that don't really solve the issue. So I figured it's time to be specific about my one.
I used to have many instances of Object.assign in my codebase. The problem is that while it correctly outputs the type of the inputs to it according it's design, I wanted something that allows me to ENFORCE the type of the first argument against subsequent arguments.
I know Object.assign takes generics, but the current definitions are limited and clumsy to use.
assign<T, U, V>
for example is rather limited, I'd prefer something that works likeassign<T, Partial<T>, Partial<T>, ...>
to allow for what I'm going for.e.g.
I have attempted to write an implementation that acts as an alternative to Object.assign, which tries to make the above behaviour happen. Important to note: it doesn't work. It's behaving practically the same way as Object.assign does. But this is just to show that I've tried to attempt a workaround, and how difficult it is to figure out:
Thanks guys, appreciate your help.
The text was updated successfully, but these errors were encountered: