Вариант 1:
type Test = {
srn: number;
qre?: unknown;
};
const test: Test = { srn: 123 };
(['srn', 'qre'] as const).forEach((p) => {
if (typeof test[p] === 'number') {
test[p] = 34;
}
});
Вариант 2:
type Test = {
srn: number
}
const test: Test = { srn: 123 };
['srn', 'qre'].forEach((p) => {
if (p === 'srn') {
test[p] = 34;
}
});
Вариант 3:
type Test = {
srn: number
}
const test: Test = { srn: 123 };
const isKeyofTest = (s: string): s is keyof Test =>
s === 'srn';
['srn', 'qre'].forEach((p) => {
if (isKeyofTest(p)) {
test[p] = 34;
}
});
https://www.typescriptlang.org/play?#code/PTAEEYCg...