这个问题在这里已有答案:
如何在C#中枚举枚举? 26个答案
public enum Foos
{
A,
B,
C
}
Run Code Online (Sandbox Code Playgroud)
有没有办法循环可能的值Foos
?
基本上?
foreach(Foo in Foos)
Run Code Online (Sandbox Code Playgroud) 我想知道如何迭代TypeScript枚举和每个枚举的符号名称.
例如,
enum myEnum { entry1, entry2 }
for (var entry in myEnum) {
// use entry's name here, e.g., "entry1"
}
Run Code Online (Sandbox Code Playgroud) 我是TypeScript的新手,我不明白我需要做些什么来修复生成TS7015错误的行(使用字符串变量引用枚举成员),因为紧跟其后的行不会出错(引用枚举成员)使用字符串文字):
enum State {
Happy = 0,
Sad = 1,
Drunk = 2
}
function Emote(enumKey:string) {
console.log(State[enumKey]); // error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
console.log(State["Happy"]); // no error
}
Run Code Online (Sandbox Code Playgroud)
"noImplicitAny": true
在项目中设置tsconfig.json
检测到错误
"noImplictAny": false
在项目中设置tsconfig.json
没有检测到错误
我正在编译 "ntypescript": "^1.201603060104.1"
我正在编译 "tsc": "1.8.10"
C:>npm install -g typescript
`-- typescript@1.8.10
Run Code Online (Sandbox Code Playgroud)
验证安装:
C:\>tsc --version
Version 1.8.10
Run Code Online (Sandbox Code Playgroud)
这是我的tsconfig.json
档案:
{
"compileOnSave": true,
"compilerOptions": {
"target": "ES5",
"module": "System",
"moduleResolution": …
Run Code Online (Sandbox Code Playgroud) 是否可以将 TypeScript 中的枚举值作为数组获取?
像这样:
enum MyEnum {
FOO = 'foo',
BAR = 'bar'
}
Run Code Online (Sandbox Code Playgroud)
变成
['foo', 'bar']
Run Code Online (Sandbox Code Playgroud) 我在typescript中定义了一个自定义文字类型:
export type Market = 'au'|'br'|'de';
Run Code Online (Sandbox Code Playgroud)
现在我想迭代每个可能Market
而不必首先创建一个数组,Market[]
因为它感觉多余,我可能忘记添加一个选项:
const markets: Market[] = ['au', 'br', 'de'];
markets.forEach((market: Market) => {
console.log(market);
});
Run Code Online (Sandbox Code Playgroud)
有没有办法用打字稿来达到这个目的?
与此问题类似,但枚举标记为常量:如何从const枚举中迭代或产生一个数组?
例
declare const enum FanSpeed {
Off = 0,
Low,
Medium,
High
}
Run Code Online (Sandbox Code Playgroud)
理想的结果
type enumItem = {index: number, value: string};
let result: Array<enumItem> = [
{index: 0, value: "Off"},
{index: 1, value: "Low"},
{index: 2, value: "Medium"},
{index: 3, value: "High"}
];
Run Code Online (Sandbox Code Playgroud) 您好我正在尝试使用TypeScript和JQuery开发一个直接的待办事项应用程序.我有一个列出任务类型的枚举:
export enum TaskType { FrontEnd, BackEnd, Designer };
Run Code Online (Sandbox Code Playgroud)
然而,使用jquery.each或for循环遍历emum,我得到以下结果,(值然后索引):
FrontEnd, BackEnd, Designer, 0, 1, 2
Run Code Online (Sandbox Code Playgroud)
以下是我通过枚举循环的代码:
constructor(e?: Object) {
var template = this.FormTemplate;
$(e).append(template);
var sel = template.find('select');
/*$.each(TaskType, function (index, el) {
sel.append("<option value='" + index + "'>" + el + "</option>");
});*/
for(var i=0; i < (typeof TaskType).length; i++){
sel.append("<option value='" + TaskType[i] + "'>" + TaskType[i] + "</option>");
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么会这样?