I am currently working on developing a dynamic function that can assess whether a particular element meets certain criteria. This project serves as an extension to the original discussion found in this question.
One challenge I am facing is that the 'key' string is no longer accessible due to lack of a signature. How can I ensure that the 'key' remains dynamic for all incoming types?
type TypeA = { id: string; equipment: string; weight: string }
type TypeB = { id: string; equipment: string; material: string }
type TypeC = { id: string; equipment: string; width: string }
type TypeD = { id: string; equipment: string; old: string }
type TypeE = { id: string; equipment: string; recent: string }
type TypeF = { id: string; equipment: string; broken: string }
type AllTypes = (TypeA | TypeB | TypeC| TypeD | TypeE | TypeF )[]
const items : AllTypes = [{ id: '11245', equipment: 'hammer', recent: 'yes' }, { id: '11335', equipment: 'screwdriver', material: 'metal' }]
const typeExclude = (key: string, el: TypeA | TypeB | TypeC| TypeD | TypeE | TypeF) => {
return key in el ? el[key as any /*the current type at hand*/] : undefined;
};
const arr = items.map(el => typeExclude('material', el) ? 'valid':'invalid')
console.log(arr)