Closed
Description
TypeScript Version: 2.2.0
Code with unexpected type error:
export function set<T, K extends keyof T>(obj: T, key: K, value: T[K]) {
return Object.assign(obj, {
[key]: value,
});
}
Expected behavior:
No type errors because K
should is a subtype of keyof T
. It works if key: keyof T
and even key: K & keyof T
.
Actual behavior:
Type error:
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
kemsky commentedon Mar 10, 2017
I was almost desperate trying to get similar code working:
Also I wonder, is it possible to avoid declaring second type parameter (
K
)?altschuler commentedon Mar 13, 2017
@kemsky You could simply replace all occurrences of
K
withkeyof T
.kevinjhanna commentedon Mar 16, 2017
Here there is another example that fails with the same error
In addition to the error @altschuler gets, I get:
Type '{ [x: string]: string; }' is not assignable to type 'Pick<State, K>'
State:
MadaraUchiha commentedon Jun 11, 2017
Bump, this still happens with 2.3.4, are there any plans to fix/implement this? cc @RyanCavanaugh (You're the only collaborator to interact with this issue)
sandersn commentedon Jul 25, 2017
The error message should be checking the apparent type of
key
. The apparent type would correctly resolve to the type parameter'sK
's constraint, which is string-like, instead of the type parameterK
itself, which is not itself string-like.sandersn commentedon Jul 25, 2017
Fixed in #17404. The apparent type turned out not be quite right because that also converts
string
toString
and so on. Instead I just get the type parameter's constraint and use that if there is one.jcalz commentedon Sep 21, 2017
The error from @kevinjhanna's comment is still occurring in TS 2.5 and above. Is this intended?
sandersn commentedon Sep 21, 2017
Yes, this needs @rbuckton's PR for binding of dynamic names to work: #15473
jcalz commentedon Jun 15, 2018
^ @sandersn
So, that error from @kevinjhanna's comment is still occurring in TS 2.9. Is this intended? Does it need a new issue?
danilofes commentedon Jun 19, 2018
Hi,
I also have a problem that may be realated to this issue (I'm using TS 2.9.2):
https://stackoverflow.com/a/50929895/2292481
davidfarinha commentedon Sep 10, 2018
Hello,
This has been implemented now but isn't in the type-system if you wanted to strongly type usages of computed properties. For example, here I'm trying to use mapped types with computed properties (I've simplified the following code/example so the purpose of the function might not make much sense practically but you can see the idea).
And attempting to strongly typing this in an ambient module (for a JS framework) and it's currently not possible. For example:
iDaN5x commentedon Mar 22, 2019
This is still occurring in TS v3.3:
Error: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
towry commentedon Jun 25, 2019
How to fix this TS kind error ?????
davidfarinha commentedon Jun 25, 2019
@towry You should be able to fix that by explicitly defining the type of the indexer as a string (number, symbol or 'any') as per the error message:
Jacob-Gray commentedon Aug 1, 2019
As far as I can tell, typescript currently can't process
keyof
a generic when used within a computed property, and instead always converts it tostring
:https://www.typescriptlang.org/play/index.html#code/MYGwhgzhAEDqBOYAOSCm8A8AVAfNA3gFDQnQCSAdhehgNLSoAeALqhQCYwDWqAngPYAzaLgAUPXgC5otADTQAbmBABXVNKwBtWgF0AlAWKljAehNx+8LhCPGSwfhQjNojaQAUw8ZgEtl2PABeAgBfAG5bO0ZNCR1oYKVVVAjI03MsXjRoAHJ8aE03aGd4HwoAcx0NbR0w6BDs6B8YCn4XSAgfMoowACMQVGhmfkHMgezPbz8QAOyAOlTSBycXKWgJ339ceMM7XfzY6US1WQWScMiQwhCgA
You can of course just
as any
them to force it to work, but that's a pretty garbage solution.towry commentedon Aug 2, 2019
@davidfarinha
sliminality commentedon Sep 25, 2019
Still a problem in 3.6 :(
vlanemcev commentedon Apr 24, 2020
REOOOOPEEEEN TAAASK!!!!!
PengBoUESTC commentedon Oct 27, 2021
how to fix this problem! plz