TypeScript-enumeration, type inference, and typescript Enumeration

Source: Internet
Author: User

TypeScript-enumeration, type inference, and typescript Enumeration
Enumeration

You can use enumeration to define some numeric constants with names. The enum keyword is used in the same way as in C.

enum Direction {    Up = 1,    Down = 1<<2,    Left,    Right}

 

Note:: An Enumeration type can have multiple enumeration members. Each enumeration member has a corresponding numeric value, which can be a constant or a calculated value. When the following conditions are met, the numeric value of the enumerated member is considered as a constant.

  • It does not have an initialization function and the previous enumerated members are constants. In this case, the value of the current enumeration member is the value of the previous enumeration member plus 1. However, the first enumeration element is an exception. If it has no initialization method, its initial value is0.
  • Enumeration member usageConstant enumeration expressionInitialization. Constant enumeration expressions are a subset of TypeScript expressions. They can be evaluated during compilation. When an expression meets the following conditions, it is a constant enumeration expression:
    • Number literal
    • Reference the previously defined constant enumeration member (which can be defined in different enumeration types). If this member is defined in the same Enumeration type, you can use a non-qualified name to reference it.
    • Constant enumeration expression with parentheses
    • +,-,~The unary operator is applied to constant enumeration expressions.
    • +,-,*,/,%,<<,>>,>>>,&,|,^Binary operator. The constant enumeration expression is used as an operation object. If the constant enumeration expression is evaluatedNaNOrInfinity, An error will be reported during the compilation phase.

 

For example:

Enum Direction {// constant Up, // 0 Down = 3, // 3 Left = Down + 4, // 7 Right, // 8 // calculated value center = [1, 2, 4]. length // 4}

 

Note:In the enumeration type, the numeric values of each enumeration member must be unique and cannot be repeated. If there are duplicates, the last

Enum Direction {// constant Up, // 0 Down = 3, // 3 Left = Down + 4, // 7 Right, // 8 // calculated value center = [1, 2, 3]. length // 3} console. log (Direction. down); // 3console. log (Direction [3]); // center

 

Enumeration types include bidirectional mappings (value-> name) and (name-> value)

console.log(Direction.Down); //3console.log(Direction[3]); //Down

 

Constant Enumeration

Constant enumeration can be used to avoid generating unnecessary code and indirect references when accessing enumeration values. Constant enumeration uses the const modifier before the enum keyword.

Constant enumeration can only use constant enumeration expressions. Unlike normal enumeration, constant enumeration is deleted during compilation. Constant enumeration members are inlined where they are used. This is because constant enumeration cannot have computing members.

Const enum Direction {// constant Up, // 0 Down = 3, // 3 Left = Down + 4, // 7 Right, // 8 // calculated value center = [1, 2, 4]. length // error}

 

Type inference

In TypeScript, where the type is not explicitly specified, type inference will help provide the type

let x = 'qwe';

The type of variable x is inferred to be string. This inference takes place when initializing variables and members, setting default parameter values and determining function return values

 

Optimal generic type

Sometimes it needs to be inferred from multiple types, then according to these types, a most suitable general type will be inferred

let x = [0, 1, null];

To InferxWe must consider the types of all elements. There are two options:numberAndnull. Calculation of general type algorithms considers all candidate types and provides a type compatible with all candidate types.

 

However, sometimes no type can be used as the type of all candidate types. If the optimal generic type is not found, the result of type inference is a null object type,{}. Because this type does not have any members, an error is reported when accessing its members.

It is generally necessary to explicitly specify the type

 

Context type

TypeScript type inference may also be performed in the opposite direction. This is called "Classification by context ". Context-based classification occurs when the expression type is related to its location.

Context categorization is used in many cases. Usually contains function parameters, the right side of the value expression, type assertions, object members, array literal and return value statements

window.onmousedown = function(mouseEvent) {    console.log(mouseEvent.buton);  //error};

The error is reported because the type checker infers the type of the parameter mouseEvent based on the value = on the right.

 

If the context type expression contains clear type information, the context type is ignored. Rewrite the example above:

window.onmousedown = function(mouseEvent: any) {    console.log(mouseEvent.buton); };

This function expression has a clear parameter type annotation, and the context type is ignored. In this case, no error is reported, because the context type is not used here.

 

 

References:

TypeScript: the superset of JavaScript

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.