The code snippet above presents an issue in TypeScript:
const exampleFn = function<AttributeName extends 'attributeA' | 'attributeB'>(
whatToProcess: AttributeName extends 'attributeA' ? {attributeA: string} : {attributeB: string},
attributeName: AttributeName
) {
//Error here: Type 'AttributeName' cannot be used to index type
//'AttributeName extends "attributeA" ? { attributeA: string; } : { attributeB: string; }'.ts(2536)
console.log(whatToProcess[attributeName]);
}
To interact with the TypeScript playground for this specific example, click here.
If the value of attributeName is 'attributeA', whatToProcess should contain attributeA. Similarly, if the value of attributeName is 'attributeB', whatToProcess should have attributeB. In each scenario, attributeName must be capable of indexing the type of whatToProcess.
Please assist me in comprehending how to correctly utilize generic and conditional typing within TypeScript, as it appears that I am missing a fundamental understanding of its functionality.