const arr = ['name', 'age', 'surname'];
// key === 'name' || age' || 'surname'
// obj:ObjType
const prepareFn = (key) => { obj[key] };
const arr = ['name', 'age', 'surname'] as const;
type arrKeys = typeof arr[number]
const obj: {[k in arrKeys]?: any} = {}
const prepareFn = (key: arrKeys) => { obj[key] };
prepareFn('name')
// @ts-expect-error
prepareFn('name2')
obj.name
// @ts-expect-error
obj.name2
enum EField {
Name = 'name',
Age = 'age',
Surname = 'surname',
};
const arr = [EField.Name, EField.Age, EField.Surname];
const obj: {[K in EField]?: any} = {}
const prepareFn = (key: EField) => { obj[key] };
prepareFn(EField.Name);
prepareFn(EField.Age);