-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generics and keyof does not work with computed properties #14473
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I was almost desperate trying to get similar code working: class Options<T extends object, K extends keyof T>
{
[key:K]:T[K];
constructor(value:T)
{
Object.assign(this, value);
}
} Also I wonder, is it possible to avoid declaring second type parameter ( |
@kemsky You could simply replace all occurrences of |
Here there is another example that fails with the same error const changeValue = <K extends keyof State> (key: K, value: string) => {
return (prevState: State, props: Props): Pick<State, K> => {
return {
[key]: value
}
}
} In addition to the error @altschuler gets, I get: State: interface State {
foo: string
} |
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) |
The error message should be checking the apparent type of |
Fixed in #17404. The apparent type turned out not be quite right because that also converts |
The error from @kevinjhanna's comment is still occurring in TS 2.5 and above. Is this intended? |
So, that error from @kevinjhanna's comment is still occurring in TS 2.9. Is this intended? Does it need a new issue? |
Hi, |
Hello, merge (target, source) {
Object.keys(source).forEach(key => {
Object.assign(target, { [key]: {} });
});
} And attempting to strongly typing this in an ambient module (for a JS framework) and it's currently not possible. For example: declare function merge<Target extends {}, Source extends {}>(target: Target, source: Source): Target extends object ?
Source extends object ? {
[Key in keyof Source]: Target & { [Key]: object } // Error here at [Key] - "'Key' only refers to a type, but is being used as a value here."
} : never
: Target; |
This is still occurring in TS v3.3: export async function* pick<TItem, TKey extends keyof TItem>(
iterable: Iterable<TItem> | AsyncIterable<TItem>,
...keys: TKey[]
): AsyncIterable<Pick<TItem, TKey>> {
for await (const item of iterable) {
yield {
[key in keys]: item[key]
};
}
}
|
@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:
|
As far as I can tell, typescript currently can't process class Wrapper<T> {
Inner<K extends keyof T>(key: K, value: T[K]) {
// All good
const x: Partial<T> = {};
x[key] = value;
// Type '{ [x: string]: T[K]; }' is not assignable to type 'Partial<T>'.
const y: Partial<T> = {
[key]: value,
};
}
} You can of course just |
Still a problem in 3.6 :( |
REOOOOPEEEEN TAAASK!!!!! |
how to fix this problem! plz |
TypeScript Version: 2.2.0
Code with unexpected type error:
Expected behavior:
No type errors because
K
should is a subtype ofkeyof T
. It works ifkey: keyof T
and evenkey: K & keyof T
.Actual behavior:
Type error:
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
The text was updated successfully, but these errors were encountered: