-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Object type not assignable to equivalent value of mapped type #25010
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
Technically, but perhaps infuriatingly, this is correct because you really don't know if an instance of |
@DanielRosenwasser I'm not sure I totally understand. So then why does |
Okay, a couple of things: First, this works the way you want under Second, really neither should work. Seems like actually that's the bug. Third,
interface Box<T> { value: T }
type Pack<T> = { [K in keyof T]: Box<T[K]> }
function patch<T, K extends keyof T>(obj: Partial<Pack<T>>, key: K, value: T[K]) {
obj[key] = { value };
}
let boxCollection = {
foo: {
value: 100,
shapeType: "cube",
},
bar: {
value: 200,
shapeType: "sphere",
}
};
patch(boxCollection, "foo", 100);
// Crash!
boxCollection.foo.shapeType.toLowerCase(); |
Thanks for the example - very helpful. If I understand correctly, the issue is that you can't just blindly assign something to function patch<T, K extends keyof T>(obj: Partial<Pack<T>>, key: K, value: T[K]) {
obj[key].value = value; // Error: Property 'value' does not exist on type 'Partial<Pack<T>>[K]'.
} |
@DanielRosenwasser any ideas here? |
With Typescript 3.1.1, this is now workable, although still not perfect. The same code as above still results in a error, but for a different reason: type Pack<T> = { [K in keyof T]: { value: T[K] } };
function patch<T, K extends keyof T>(obj: Partial<Pack<T>>, key: K, value: T[K]) {
obj[key].value = value; // Error: Type 'T[K]' is not assignable to type 'T[string] | T[number] | T[symbol]'.
} I'm not sure if this behavior is expected or not. Either way, if I enforce that type Pack<T> = { [K in keyof T]: { value: T[K] } };
function patch<T extends {[key: string]: any}, K extends keyof T>(obj: Partial<Pack<T>>, key: K, value: T[K]) {
obj[key].value = value; // Works!
}
type SomeType = {
a: number, b: string
};
const obj: Partial<Pack<SomeType>> = {
a: { value: 1 }
}
// With type checking and everything!
patch(obj, 'b', 2); // Error: Argument of type '2' is not assignable to parameter of type 'string'. Will leave it up to you the maintainers whether to close based on this information. I don't fully understand what issues are outstanding here and which ones are actually issues in the first place. |
Closing as the original example is no longer an error. |
Code
Example usage of
patch()
:Expected behavior:
Definition of
patch()
function should be fine, and calling should produce the desired outcome.Actual behavior:
patch()
function fails to compile with errorType '{ value: T[K]; }' is not assignable to type 'Pack<T>[K]'
even though{ value: T[K] }
does appear to be assignable toPack<T>[K]
:Playground Link:
Additional examples here: https://bit.ly/2le5qZq
The text was updated successfully, but these errors were encountered: