Why typescript create warning when I copy already narrowed object property to the other object.
interface View {
x: string;
y: boolean;
}
type viewKeys = keyof View;
const view: View = {
x: 's',
y: true,
}
const allowedKeys: viewKeys[] = ['x'];
const derivedDataString: string | null = '{ "x": true }';
const derivedData: unknown = derivedDataString ? JSON.parse(derivedDataString) : null;
if (derivedDataIsCorrect(derivedData)) {
allowedKeys.forEach((k) => {
// warning shown here about view[k] can't be assigned to 'never'
view[k] = derivedData[k];
});
}
function derivedDataIsCorrect (data: unknown): data is Partial<View> {
if (!data) {
return false;
}
const _data = data as Partial<View>;
return typeof _data.x === 'string' && typeof _data.y === 'boolean';
}