【发布时间】:2023-03-24 14:05:01
【问题描述】:
在我的班级我有一个财产:
thousandSeparator: '' | ',' | ' ' | '.';
并希望通过以下方式设置它:
const options = {
thousandSeparator: ','
};
设置时出现错误
Types of property 'thousandSeparator' are incompatible.
Type 'string' is not assignable to type '"" | "," | " " | "."'.
【问题讨论】:
-
options.thousandSeparator应该是 const 如果你想将它分配给类型'' | ',' | ' ' | '.' -
TLDR:使用
const options = { thousandSeparator: ',' as const };所以','实际上是','类型,并且没有扩大到'string'类型。
标签: typescript