Getting an error in the following example at the return on the class method.
<code>Type 'K' cannot be used to index type 'A'.ts(2536)
</code>
<code>Type 'K' cannot be used to index type 'A'.ts(2536)
</code>
Type 'K' cannot be used to index type 'A'.ts(2536)
<code>interface A {
a: string;
b: string;
}
interface Test {
attributes: A;
}
const test: Test = {
attributes: {
a: '123',
b: '456',
},
};
class Something<T extends Test = Test> {
getAttribute<K extends keyof T['attributes'] = keyof T['attributes']>(name: K): T['attributes'][K] | undefined {
return test.attributes[name];
}
}
</code>
<code>interface A {
a: string;
b: string;
}
interface Test {
attributes: A;
}
const test: Test = {
attributes: {
a: '123',
b: '456',
},
};
class Something<T extends Test = Test> {
getAttribute<K extends keyof T['attributes'] = keyof T['attributes']>(name: K): T['attributes'][K] | undefined {
return test.attributes[name];
}
}
</code>
interface A {
a: string;
b: string;
}
interface Test {
attributes: A;
}
const test: Test = {
attributes: {
a: '123',
b: '456',
},
};
class Something<T extends Test = Test> {
getAttribute<K extends keyof T['attributes'] = keyof T['attributes']>(name: K): T['attributes'][K] | undefined {
return test.attributes[name];
}
}
However, if I just directly put keyof A
:
<code>class Something<T extends Test = Test>{
getAttribute<K extends keyof A = keyof A>(name: K): T['attributes'][K] | undefined {
return test.attributes[name];
}
}
</code>
<code>class Something<T extends Test = Test>{
getAttribute<K extends keyof A = keyof A>(name: K): T['attributes'][K] | undefined {
return test.attributes[name];
}
}
</code>
class Something<T extends Test = Test>{
getAttribute<K extends keyof A = keyof A>(name: K): T['attributes'][K] | undefined {
return test.attributes[name];
}
}
It seems happy. Is there a way to use the former method and infer the type of attributes
?