-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Computed property key names should not be widened #13948
Comments
That is correct. |
Ok, so the above React example can never work, because |
@sebald You might mean something like let peter: Pick<Partial<Person>, 'name' | 'age'> = {
[key]: value
}; In any case, I don't think |
@DanielRosenwasser thanks for the quick reply! I was wondering if using For everyone that has the same issue: As a temporary fix you can write |
This looks like a duplicate of #15534 |
Partial wouldn't be ideal either because you could then update all non nullable properties with undefined. Btw you posted a workaround that looks identical to the original code? |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
with #18317 in the simple case with a single literal type works, but a union of literals still does not. we should distribute the union over the computed property. |
cross-linking to #21030 |
This is clearly a very old question, and a lot has changed. You may find the following useful (feel free to skip to the accepted answer!) |
Now that TypeScript supports pattern template literal index signatures, this widening to function foo(str: string) {
const key = `prefix_${str}` as const;
// const key: `prefix_${string}`
const obj = { [key]: 123 }
// const obj: { [x: string]: number; } 😢
// wanted const obj: { [x: `prefix_${string}]: number; }
} |
This makes computed properties error when used with mapped types too. function partial<T, P extends keyof T>(field: P, value: T[P]): Partial<T> {
return {
[field]: value,
};
// Type '{ [x: string]: T[P]; }' is not assignable to type 'Partial<T>'.(2322)
} |
FWIW this is the helper function I tend to use when I need stronger types for computed keys: function kv<K extends PropertyKey, V>(k: K, v: V): { [P in K]: { [Q in P]: V } }[K] {
return { [k]: v } as any
} Which produces these:
|
TypeScript Version: 2.1.5
Code
The latest
@types/react
(v15.0.6
) usePick<S,K>
to correctly type thesetState
method ofReact.Component
s. While this makes it now possible to merge thestate
of a component instead of replacing it, it also makes it harder to write a dynamic update function that uses computed properties.The above should show an actual use case of the issue, but it can be reduced to:
which will result in the same error. Link to the TS playground
Expected behavior:
No error, because
key
is akeyof Person
, which will result in the literal type"name" | "age"
. Both values that are valid keys forstate
.Actual behavior:
The compiler will throw the following error:
My uninformed guess is that the constant
key
is (incorrectly) widened tostring
.The text was updated successfully, but these errors were encountered: