在我的课堂上,我有一个财产:
thousandSeparator: '' | ',' | ' ' | '.';
并希望通过以下方式进行设置:
const options = {
thousandSeparator: ','
};
设置这个时我得到错误
Types of property 'thousandSeparator' are incompatible.
Type 'string' is not assignable to type '"" | "," | " " | "."'.
您的代码在tsc操场上工作正常
如果您的代码有更多内容,例如在将字符串设置为变量之前在其他地方定义字符串,则必须将其强制转换,如下所示:
type thousandSeparator = '' | ',' | ' ' | '.';
const options = {
thousandSeparator: ',' as thousandSeparator,
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句