这是我当前的代码:
interface sizes {
[key: string]: Partial<CSSStyleDeclaration>[];
}
export const useStyleBlocks = (
resolution = 'large',
blocks = [{}]
): Partial<CSSStyleDeclaration>[] => {
const starterBlock = [] as Partial<CSSStyleDeclaration>[];
const handleBlocks = (key: sizes): Partial<CSSStyleDeclaration>[] => {
for (let i = 0; i < blocks.length; i++) {
// the error comes from this line exactly on [key]
// A computed property name must be of type 'string', 'number', 'symbol', or 'any'
starterBlock.push({ [key]: blocks });
}
return starterBlock;
};
switch (resolution) {
case 'small':
// then here i get another error
// Argument of type 'string' is not assignable to parameter of type 'sizes'
handleBlocks('small');
break;
default:
handleBlocks({});
break;
}
return starterBlock;
};
有关错误的解释在上面的代码中加注了^
有任何想法吗?
更新:
如果我将功能更改handleBlocks
为:
const handleBlocks = (key: string): Partial<CSSStyleDeclaration>[] => {
for (let i = 0; i < blocks.length; i++) {
/* HERE I GET A NEW ERROR:
Argument of type '{ [x: string]: {}[]; }' is not assignable to parameter of type 'Partial<CSSStyleDeclaration>'.
Index signatures are incompatible.
Type '{}[]' is not assignable to type 'string'.ts(2345) */
starterBlock.push({ [key]: blocks });
}
return starterBlock;
};
有了这一更改,switch
语句上的错误就消失了。
这行:
const handleBlocks = (key: sizes): Partial<CSSStyleDeclaration>[] => {
的意思是handleBlocks
想要一个类型为实参的参数sizes
,该接口是一个对象,但是当您调用时handleBlocks
,您正在传入一个字符串(“ small”)。
然后,您抱怨您试图将对象类型用于计算的属性名称,因为key
它的类型sizes
是,即对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句