简体   繁体   English

TypeScript 中是否可以有数字字符串类型?

[英]Is it possible to have a numeric string type in TypeScript?

Update: I now realize that I had overlooked how strings are handled during runtime, so 100 + 5 wouldn't actually work like I had expected.更新:我现在意识到我忽略了在运行时如何处理字符串,所以 100 + 5 实际上不会像我预期的那样工作。 I'll leave this up to see if anyone has a better solution for my issue that's not type casting or type definitions.我会留下来看看是否有人对我的问题有更好的解决方案,而不是类型转换或类型定义。

I've been looking all over, so I apologize if this is a duplicate question, however I was wondering if it is possible to define a TypeScript type for numerical strings that takes either a string or a value and then indicates it will always be a number?我一直在寻找,所以如果这是一个重复的问题,我深表歉意,但是我想知道是否可以为数字字符串定义一个 TypeScript 类型,该类型接受一个字符串或一个值,然后表明它总是数字? I work with sending data to a number of different systems and, unfortunately, I have no control over the model definitions (they're controlled by the various teams that own the data).我将数据发送到许多不同的系统,不幸的是,我无法控制模型定义(它们由拥有数据的各个团队控制)。 This leaves me with having to cast all the strings to numbers to do calculations and then back to strings to keep the IDE happy and results in a lot of extra work.这让我不得不将所有字符串转换为数字来进行计算,然后再转换回字符串以保持 IDE 的运行,并导致大量额外的工作。

I'd love if I could do something like the following, it would help reduce the amount of redundant work:如果我可以执行以下操作,我会很高兴,这将有助于减少冗余工作量:

type NumericalString<T extends number | string> = T extends (string | number) ? number : never;

let someVal: NumericalString = "100"; // Recognized as 100 by IDE

// ... elsewhere in code
if (isNumericalString(someVal)) {
    someVal+= 5; // No need to cast to number because IDE already knows it's a number
}

I'd especially love it if there was a way for TypeScript to be smart and know that "100" was a valid numeric string, but that "Foobar" wasn't, but I'd settle for being able to trick the IDE into thinking ALL strings were numbers when assigned as NumericStrings and deal with the ramifications of that, given that I'm the only one actively working on this project.如果 TypeScript 有办法变得聪明并且知道“100”是一个有效的数字字符串,但“Foobar”不是,我会特别喜欢它,但我愿意接受能够欺骗 IDE 进入考虑到我是唯一一个积极参与这个项目的人,认为所有字符串在分配为 NumericStrings 时都是数字并处理其后果。

I appreciate any help on this, I feel like I lose a good hour / hour and a half a day to this and would love to boost my productivity, if able.我很感激这方面的任何帮助,我觉得我为此浪费了一个小时/一个半天的时间,如果可以的话,我很想提高我的工作效率。

Here both numbers and string as number will be accepted but any other string can not be accepted这里将接受数字和字符串作为数字,但不能接受任何其他字符串

type NumericalString = `${number}` | number;

let str: NumericalString = "120"; // OK
let num: NumericalString = 120; // OK

let someOtherStr: NumericalString = "Any Thing Than Num"; // Show error by IDE

I assume you want to overload the += operator.我假设您想重载+=运算符。 This isn't supported by TypeScript at the moment.目前 TypeScript 不支持此功能。 You have to cast that manually, as far as I know.据我所知,您必须手动投射。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在TypeScript中接口可以同时具有数字和字符串索引签名? - Why is it possible to have both numeric and string index signatures for an interface in TypeScript? 如何在打字稿中使用可能的索引以及字符串和数字索引来确定对象的类型? - How to determine the type of object with possible and string and numeric indices in typescript? 是否可以在 TypeScript 中的类型定义中进行类型检查 - Is it possible to have a type check in type definition in the TypeScript Typescript:类型“字符串”不可分配给类型“数字”| Date::toLocaleDateString() 中的“2 位” - Typescript: Type 'string' is not assignable to type '"numeric" | "2-digit"' in Date::toLocaleDateString() 是否可以在 Typescript 中为每个映射类型设置一个泛型 - Is it possible to have a Generic for each Mapped Type in Typescript 打字稿中是否可以有一个联合类型的构造函数对象? - Is it possible to have a union type constructor object in typescript? 如何在打字稿中为数字枚举提供替代字符串定义 - How to have alternative string definition for numeric enum in typescript "是否可以在 TypeScript 中输入检查字符串别名?" - Is it possible to type check String Aliases in TypeScript? Typescript:是否可以将泛型类型评估为字符串文字? - Typescript: Is it possible to evaluate generic type into string literal? 是否可以使用字符串值来引用 Typescript 中的类型 - Is it possible to use a string value to refer to a type in Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM