TypeScript 字符类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42678891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript character type?
提问by charliebrownie
My question is very brief. I am new to TypeScript, been searching around here and there but didn't find yet an answer.
我的问题很简短。我是 TypeScript 的新手,一直在四处寻找,但还没有找到答案。
Does any experienced TypeScripter know if there is a character Type or an easy way to achieve one?
有没有经验丰富的 TypeScripter 知道是否有字符 Type 或实现一个的简单方法?
采纳答案by Ryan Cavanaugh
TypeScript does not have a type for representing fixed-length strings.
TypeScript 没有用于表示固定长度字符串的类型。
回答by rossipedia
I'm not sure about easy, but you could sorta do something with string literal types:
我不确定easy,但你可以用字符串文字类型做一些事情:
type Char = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k'
| 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x'
| 'y' | 'z' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K'
| 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X'
| 'Y' | 'Z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' // etc....;
of course, this is a fairly brittle solution and breaks down when you consider unicode characters, and I wouldn't really suggest it. As Ryan mentions, JavaScript itself doesn't have any notion of a fixed length string, nor the concept of a char
as distinct from a string
.
当然,这是一个相当脆弱的解决方案,当您考虑 unicode 字符时会崩溃,我真的不建议这样做。正如 Ryan 提到的,JavaScript 本身没有任何固定长度字符串的概念,也没有char
区别于 a的概念string
。
回答by cocoseis
A Char is simply a number. Since the basic types of TypeScript do not include Chars what you could do is store a number and convert it back and forth:
Char 只是一个数字。由于 TypeScript 的基本类型不包括 Chars,您可以做的是存储一个数字并将其来回转换:
var number = "h".charCodeAt(0);
var char = String.fromCharCode(number)
And to something like:
和类似的东西:
class Char {
private _value:Number;
constructor(char: Number | String){
this.setValue(char);
}
get getValue():String {
return String.fromCharCode(this._value);
}
set setValue(char: Number | String) {
if (typeof char === "number") {
this._value = char;
}
else {
this._value = char.charCodeAt(0);
}
}
}
回答by Andreas
You could use a regex within TypeGuard to contain it's type eg:(you can declare an empty enum to get a new type to be associated with the type guard)
您可以在 TypeGuard 中使用正则表达式来包含它的类型,例如:(您可以声明一个空枚举以获得与类型保护关联的新类型)
enum CharType { }
export type Char = string & CharType
const isChar=(str: string): str is Char => /^(.|\n)$/.test(
str
)
export char(c:string):Char{
//you can also use is char here for to test whether actually is char
if (!isChar(c)){
throw new Error('not a char')
}
return c
}
Now Char matches only things that come from calling char(eg actually casted by calling the function rather than just asserted on build time. The compiler simply accepts that a Char is returned and that's actually true if you think of it since it will just throw otherwise)
现在 Char 只匹配来自调用 char 的东西(例如,实际上是通过调用函数而不是在构建时断言。编译器简单地接受返回一个 Char,如果你想到它,这实际上是正确的,因为它只会抛出否则)
Original Source(applied to date string): Atomic Object
原始来源(应用于日期字符串): 原子对象
Assumptions: I assume that by mentioning typescript you mean a type used for compile-time checks by typescript compiler and not looking for any kind of optimization on the actual compiled js side(since js only has strings)
假设:我假设提到 typescript 是指 typescript 编译器用于编译时检查的类型,而不是在实际编译的 js 端寻找任何类型的优化(因为 js 只有字符串)
The only issue I can see is that you can pass whatever to char function and it will only throw at run time. But you will never reach to a state where you expect a Char and you get something else(since Chars only come from calling char).
我能看到的唯一问题是您可以将任何内容传递给 char 函数,它只会在运行时抛出。但是你永远不会达到你期望一个 Char 而你得到其他东西的状态(因为 Chars 只来自调用 char)。
On a side note even java casts throw just runtime exceptions.
附带说明一下,即使 java 强制转换也只抛出运行时异常。
Although the above approach might not have much to do with casts, I do find some commonalities...
虽然上述方法可能与演员没有太大关系,但我确实发现了一些共同点......
回答by zhangjinzhou
As I know, the basic types of typescript don't include char.
据我所知,打字稿的基本类型不包括字符。