简体   繁体   English

TypeScript:元素隐含地具有 RegExp 的“任何”类型

[英]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 需要一个持续时间字符串(例如12ms7.5 MIN400H ),解析它并将其转换为毫秒。

 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.

相关问题 TypeScript 元素隐含一个“任何” - TypeScript Element implicitly has an 'any' 映射打字稿参数:绑定元素“列”隐式具有“任何”类型 - Map typescript parameter: Binding element 'columns' implicitly has an 'any' type React typescript 错误:元素隐式具有“任何”类型 - React typescript error: Element implicitly has an 'any' type TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type Typescript,联合类型,元素隐含“任何” - Typescript, Union types, Element implicitly has an 'any' RN打字稿中参数隐式具有任何类型 - Parameter implicitly has any type in RN typescript 打字稿:可能导致此错误的原因是什么? “元素隐式具有&#39;任意&#39;类型,因为类型&#39;对象&#39;没有索引签名” - Typescript: what could be causing this error? “Element implicitly has an 'any' type because type 'Object' has no index signature” 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript TypeScript - 元素隐式具有“任何”类型 [...] 在类型上未找到具有“字符串”类型参数的索引签名 - TypeScript - Element implicitly has an 'any' type [...] No index signature with a parameter of type 'string' was found on type TypeScript - 元素隐式具有“任意”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM