Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs TS2345对象args中的键像[someKey]一样写入时出错_Reactjs_Typescript_Tsc_Ts Loader - Fatal编程技术网

Reactjs TS2345对象args中的键像[someKey]一样写入时出错

Reactjs TS2345对象args中的键像[someKey]一样写入时出错,reactjs,typescript,tsc,ts-loader,Reactjs,Typescript,Tsc,Ts Loader,什么时候 在[someVariable]中,它说 TS2345:类型为{fontWeight:“bold”的参数;线宽:数字; textAlign:“居中”…“不是 可分配给“CSSProperties”类型的参数。对象文字可以 仅指定已知属性,而“[someVariable]”不指定 存在于“CSSProperties”类型中 如何修复此问题?如果someVariable是字符串文字类型,而该类型不是React.cssprroperties的属性,则会发生这种情况 function Style

什么时候

[someVariable]
中,它说

TS2345:类型为{fontWeight:“bold”的参数;线宽:数字; textAlign:“居中”…“不是 可分配给“CSSProperties”类型的参数。对象文字可以 仅指定已知属性,而“[someVariable]”不指定 存在于“CSSProperties”类型中


如何修复此问题?

如果
someVariable
是字符串文字类型,而该类型不是
React.cssprroperties的属性,则会发生这种情况

function StyleMixin(base: React.CSSProperties) {}




StyleMixin({
    fontWeight: 'bold',
    lineHeight: 1,
    textAlign: 'center',
    [someVariable]: {
        fontSize: '1rem',
    }
}
如果
someVariable
是一个变量而不是一个常量(即用
let
var
声明),它实际上会起作用

我建议确保您确实想要添加一个不在
cssprroperties
中的属性(如果您没有看到完整的错误消息,请使用tsconfig.json中的
“noErrorTruncation”:true

如果您确实希望
StyleMixin
成为可以向
cssprroperties
添加额外属性的对象,则可以在函数中使用泛型参数:

const someVariable = "nonExistentProperty";
StyleMixin({
    fontWeight: 'bold',
    lineHeight: 1,
    textAlign: 'center',
    [someVariable]: {
        fontSize: '1rem',
    }
})
函数StyleMixin(base:T){}
const someVariable=“不存在属性”;
StyleMixin({
fontWeight:'粗体',
线宽:1,
textAlign:'中心',
[某些变量]:{
fontSize:'1rem',
}
})

如果
someVariable
是字符串文字类型,而不是
React.cssprroperties的属性,则会发生这种情况

function StyleMixin(base: React.CSSProperties) {}




StyleMixin({
    fontWeight: 'bold',
    lineHeight: 1,
    textAlign: 'center',
    [someVariable]: {
        fontSize: '1rem',
    }
}
如果
someVariable
是一个变量而不是一个常量(即用
let
var
声明),它实际上会起作用

我建议确保您确实想要添加一个不在
cssprroperties
中的属性(如果您没有看到完整的错误消息,请使用tsconfig.json中的
“noErrorTruncation”:true

如果您确实希望
StyleMixin
成为可以向
cssprroperties
添加额外属性的对象,则可以在函数中使用泛型参数:

const someVariable = "nonExistentProperty";
StyleMixin({
    fontWeight: 'bold',
    lineHeight: 1,
    textAlign: 'center',
    [someVariable]: {
        fontSize: '1rem',
    }
})
函数StyleMixin(base:T){}
const someVariable=“不存在属性”;
StyleMixin({
fontWeight:'粗体',
线宽:1,
textAlign:'中心',
[某些变量]:{
fontSize:'1rem',
}
})