有 2 个具有相同类型的对象。一个对象(我们称之为“fulledObj”)最初被填充并遍历键。所有找到的属性都必须在迭代期间写入另一个对象(我们称之为“needfulProps”)。我必须马上说,将一个对象分配给另一个/克隆的解决方案是不合适的。我需要在 needfulProps 对象上覆盖/创建一个属性。
* 非工作部分在代码中标有注释:
type responsiveArguments = {
heightRow?: string,
widthColumn?: string,
countColumns?: number,
countRows?: number,
rowGap?: number
}
let needfulResponsiveProps: responsiveArguments = {};
let oneWidthResponsiveObj: responsiveArguments = {
heightRow: "300px",
widthColumn: "500px",
countRows: 2
}
for (const keyResponsiveObj of Object.keys(oneWidthResponsiveObj) as (keyof responsiveArguments)[]) {
// Вывести в консоль объект с этим свойством я могу
console.log(`needfulResponsiveProps[keyResponsiveObj]: `, needfulResponsiveProps[keyResponsiveObj]);
// Присвоить не могу
// Тип "string | number | undefined" не может быть назначен для типа "undefined". Тип "string" не может быть назначен для типа "undefined"
needfulResponsiveProps[keyResponsiveObj] = oneWidthResponsiveObj[keyResponsiveObj];
}
此外,如果对象类型中的所有字段都属于同一类型,则一切正常:
type exampleType = { //все поля одного типа(number)
num?: number,
num2?: number
}
const filledObj: exampleType = {
num: 10,
num2: 15
}
let testConst: exampleType = {};
for (const key of Object.keys(filledObj) as (keyof exampleType)[]) {
testConst[key] = filledObj[key];
}
完整的代码,从中可以清楚为什么简单的对象复制是不合适的:
type responsive = {
[resolution: number]: responsiveArguments;
}
type responsiveArguments = {
heightRow?: string | undefined;
widthColumn?: string | undefined;
countColumns?: number | undefined;
countRows?: number | undefined;
rowGap?: number | undefined;
}
/* for test */viewWidthDevice = 1500; // ширина устройства - будет считаться автоматически
/* for test */responsive = {
500: {
countColumns: 500,
countRows: 2
},
800: {
countRows: 50,
heightRow: "500px"
},
1900: {
countColumns: 50
}
}
let objPropsLessWidth: responsive = {};
if(responsive) {
for(let i = 0; i < viewWidthDevice; i++) {
if(responsive.hasOwnProperty(i)) {
objPropsLessWidth[i] = responsive[i];
};
}
console.log("objPropsLessWidth: ", objPropsLessWidth); //ключ 1900 будет убран, ведь ширин
if(Object.keys(objPropsLessWidth).length > 0) {
let needfulResponsiveProps: responsiveArguments = {};
for (const keyOneResponsiveWidth in objPropsLessWidth) { // enumeration all responsive appropriate properties
const oneWidthResponsiveObj: responsiveArguments = objPropsLessWidth[keyOneResponsiveWidth];
//надо так:
for (const keyResponsiveObj of Object.keys(oneWidthResponsiveObj) as (keyof responsiveArguments)[]) {
//в левом операнде ошибка
//Тип "string | number | undefined" не может быть назначен для типа "undefined". Тип "string" не может быть назначен для типа "undefined".
needfulResponsiveProps[keyResponsiveObj] = oneWidthResponsiveObj[keyResponsiveObj];
}
//хочу избавиться от этого промежутка кода в сторону цикла, который выше
if(oneWidthResponsiveObj.countColumns) needfulResponsiveProps.countColumns = oneWidthResponsiveObj.countColumns;
if(oneWidthResponsiveObj.countRows) needfulResponsiveProps.countRows = oneWidthResponsiveObj.countRows;
if(oneWidthResponsiveObj.heightRow) needfulResponsiveProps.heightRow = oneWidthResponsiveObj.heightRow;
if(oneWidthResponsiveObj.rowGap) needfulResponsiveProps.rowGap = oneWidthResponsiveObj.rowGap;
if(oneWidthResponsiveObj.widthColumn) needfulResponsiveProps.widthColumn = oneWidthResponsiveObj.widthColumn;
}
}
}
使用循环,让 ts 理解类型将不起作用(至少我不知道方法):操场
但您可以使用演员阵容:操场
你也可以使用一个函数来化学,但我不会,因为这是一堆额外的代码:操场