How to create enum like type in TypeScript?

后端 未结 6 1098
南旧
南旧 2020-12-08 09:07

I\'m working on a definitions file for the Google maps API for TypeScript.

And I need to define an enum like type eg. google.maps.Animation which contai

6条回答
  •  再見小時候
    2020-12-08 09:43

    As of TypeScript 0.9 (currently an alpha release) you can use the enum definition like this:

    enum TShirtSize {
      Small,
      Medium,
      Large
    }
    
    var mySize = TShirtSize.Large;
    

    By default, these enumerations will be assigned 0, 1 and 2 respectively. If you want to explicitly set these numbers, you can do so as part of the enum declaration.

    Listing 6.2 Enumerations with explicit members

    enum TShirtSize {
      Small = 3,
      Medium = 5,
      Large = 8
    }
    
    var mySize = TShirtSize.Large;
    

    Both of these examples lifted directly out of TypeScript for JavaScript Programmers.

    Note that this is different to the 0.8 specification. The 0.8 specification looked like this - but it was marked as experimental and likely to change, so you'll have to update any old code:

    Disclaimer - this 0.8 example would be broken in newer versions of the TypeScript compiler.

    enum TShirtSize {
      Small: 3,
      Medium: 5,
      Large: 8
    }
    
    var mySize = TShirtSize.Large;
    

提交回复
热议问题