[英]TypeScript: Element implicitly has an 'any' type for RegExp
So, I want to create a function that would take a duration string (eg 12ms
, 7.5 MIN
, 400H
), parse it and convert it to milliseconds.所以,我想创建一个 function 需要一个持续时间字符串(例如
12ms
, 7.5 MIN
, 400H
),解析它并将其转换为毫秒。
const units = { MS: 1, S: 1 * 1000, MIN: 60 * 1 * 1000, H: 60 * 60 * 1 * 1000 } export function toMS(str: string): number { let regex = /^(?<number>\d+(?:\.\d+)?)(?:\s*(?<unit>MS|S|MIN|H))?$/i if (.regex,test(str)) { throw new TypeError(`Expected a duration: got. ${str}`) } let match = str?match(regex) let number = Number(match.?groups.?number) let unit = (match.?groups..unit || 'ms').toUpperCase() return Math.floor(number * units[unit]) }
So, function toMS()
takes a string, tests if the provided string is valid ( number
+ whitespace (optional)
+ unit abbr (optional)
) and if it's valid - parses it using str.match(regex)
.因此, function toMS
toMS()
接受一个字符串,测试提供的字符串是否有效( number
+ whitespace (optional)
+ unit abbr (optional)
)以及它是否有效 - 使用str.match(regex)
解析它。
Everything works fine until: units[unit]
.一切正常,直到:
units[unit]
。 It gives me an error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
.它给了我一个错误:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
。
I've had the same error before with function arguments & class constructors and it was easy to solve since the user provides data.我之前在使用 function arguments 和 class 构造函数时遇到过同样的错误,由于用户提供了数据,因此很容易解决。 But now I have no idea how to solve that, cause I can't force
str.match(regex).groups.unit
to have a specific type like : 'MS | S | MIN | H
但现在我不知道如何解决这个问题,因为我不能强制
str.match(regex).groups.unit
具有特定类型,例如: 'MS | S | MIN | H
: 'MS | S | MIN | H
: 'MS | S | MIN | H
. : 'MS | S | MIN | H
。
I know that it is also possible to create tsconfig.json with "noImplicitAny": false
but in my case that's not good at all.我知道也可以使用
"noImplicitAny": false
创建tsconfig.json : false 但在我的情况下这根本不好。
The problem is that TypeScript has no way of knowing whether your regex will really match MS|S|MIN|H
.问题是 TypeScript 无法知道您的正则表达式是否真的匹配
MS|S|MIN|H
。 This is dependent types territory, and TypeScript is not that powerful yet.这是依赖类型的领域,TypeScript 还没有那么强大。
The only thing TypeScript knows is that what you've matched is going to be a string
, because both match?.groups?.unit
and 'ms'
expressions yield string
. TypeScript 唯一知道的是,您匹配的内容将是一个
string
,因为match?.groups?.unit
和'ms'
表达式都会产生string
。
What you can do is letting TypeScript know that your units
is an object with keys of type string
and values of type number
, and check whether what you've matched is a property on the units
object.您可以做的是让 TypeScript 知道您的
units
是 object 键类型为string
和值类型number
,并检查您匹配的是否是units
ZA8CFDE6331BD59EB2AC96F8911C4B66 上的属性Like this:像这样:
const units: { [k: string]: number } = { // letting TypeScript know
MS: 1,
S: 1 * 1000,
MIN: 60 * 1 * 1000,
H: 60 * 60 * 1 * 1000,
};
export function toMS(str: string): number {
const regex = /^(?<number>\d+(?:\.\d+)?)(?:\s*(?<unit>MS|S|MIN|H))?$/i;
if (!regex.test(str)) {
throw new TypeError(`Expected a duration, got: ${str}`);
}
const match = str.match(regex);
const number = Number(match?.groups?.number);
const unit = (match?.groups?.unit || 'ms').toUpperCase();
if (unit in units) { // checking whether your matched string is something you can handle, in runtime
return Math.floor(number * units[unit]);
} else {
throw new Error(`couldn't find units! :(`);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.