相关疑难解决方法(0)

Javascript枚举到对应的字符串值

所以我在我的页面的javascript中有这个:

var TEST_ERROR  = {
        'SUCCESS'   :   0,
        'FAIL'      :   -1,
        'ID_ERROR'  :   -2
      };
Run Code Online (Sandbox Code Playgroud)

并对页面中的函数执行测试,如下所示:

function test()
{
    // Get the paragraph from the DOM
    var testResultParagraph = document.getElementById('testResult');

    // Check the paragraph is valid
    if(!testResultBox)
    {
        // Update the web page
        testResultParagraph.value = TEST_ERROR.ID_ERROR;
        return TEST_ERROR.ID_ERROR;
    }

    // Something to store the results
    var testResult = TEST_ERROR.SUCCESS;

    // Test the calculation
    testResult = testCalculate()

    // Update the web page
    testResultParagraph.value = testResult;

    // The test succeeded
    return TEST_ERROR.SUCCESS; …
Run Code Online (Sandbox Code Playgroud)

javascript enums

26
推荐指数
4
解决办法
6万
查看次数

如何在打字稿中使用枚举作为索引键类型?

考虑以下示例.

enum DialogType {
    Options,
    Help
}

class Dialog { 
    test() : string {
        return "";
    }
}

class Greeter {

    openDialogs: { [key in DialogType]: Dialog | undefined } = {
        0: undefined,
        1: undefined
    };

    getDialog(t: DialogType) {
        return this.openDialogs[t];
    }
}

const greeter = new Greeter();
const d = greeter.getDialog(DialogType.Help);
if (d) document.write(d.test());
Run Code Online (Sandbox Code Playgroud)

也在操场上

它有3个问题/问题:

  1. 为什么我不能在初始化文字中省略属性,即使我将属性声明为'| 未定义"
  2. 为什么我不能使用'DialogType.Options'作为类型键,而必须使用硬编码?
  3. 为什么我必须使用'key in DialogType'而不是'key:DialogType'?(或者我可以吗?)

indexing enums dictionary typescript

16
推荐指数
1
解决办法
8792
查看次数

在Typecript中使用Enum作为受限密钥类型

问题是可以枚举用作键类型而不仅仅是"数字"或"字符串"?目前似乎唯一可能的声明是x:{[key:number]:any}其中key可以是"number"或"string"类型.有可能在这个例子中做出类似的东西:

例:

enum MyEnum
{
    First,
    Second
}

var layer:{[key:MyEnum]:any};
Run Code Online (Sandbox Code Playgroud)

typescript

13
推荐指数
5
解决办法
3506
查看次数

如何在Typescript中使用枚举键定义哈希

我正在使用TypeScript 制作RAM机器模拟器,因此我制作了RAM可以具有的指令类型的枚举:

enum InsType {
    LOAD,   // Put value from specified register (or literal) into accumulator.
    STORE,  // Store value from accumulator into specified register.
    READ,   // Read from input tape and write into specified register.
    WRITE,  // Write to output tape from specified register.
    ADD,    // Add value into accumulator.
    SUB,    // Subtract value from accumulator.
    MUL,    // Multiply accumulator by referenced (or literal) value.
    DIV,    // Divide accumulator by referenced (or literal) value.
    HALT,   // Stop …
Run Code Online (Sandbox Code Playgroud)

hash enums typescript

12
推荐指数
4
解决办法
6877
查看次数

标签 统计

enums ×3

typescript ×3

dictionary ×1

hash ×1

indexing ×1

javascript ×1