Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rules for the use of angle brackets in TypeScript

Tags:

typescript

What are the general rules that dictate when, where, how and why angle brackets, i.e. <...>, should be used in TypeScript?

While I think I understand many individual uses of these brackets, I have a hard time seeing general patterns for their use: they sometimes seem to be prepended before things, sometimes appended after things; sometimes they are used for generics, sometimes for specific types; sometimes they appear where I might have expected the colon syntax to be used.

I'd like to have a concise but exhaustive/universal explanation of what the brackets mean, their exact syntax, when they should be used, when they shouldn't be used, etc.

like image 668
Andrew Willems Avatar asked May 21 '16 02:05

Andrew Willems


People also ask

What do the angled brackets mean in typescript?

One within the parentheses () and another within the angle brackets < > We know in the function call func(12) , argument 12 within the parentheses represents the arg parameter. Similarly in func<number>(12) , the argument number within the angle brackets represents the generic type parameter T .

What are the angle brackets used for?

Usage of angle brackets In English, they may be used informally to insert asides, indicate speech in a foreign language, or to mention a website, but all of these uses are rare even in informal writing.

What is angle bracket in Javascript?

As you probably know, angle brackets in pairs are used: As a deprecated syntax for type-assertions. For manually specifying generic type parameters. For declaring elements in .

What do angle brackets mean in coding?

What Does Angle Bracket Mean? The angle bracket (< or >), which is also called an “inequality sign” for its use in mathematics, is a kind of sideways caret that can be used to include tags or pieces of code. This ASCII set of characters is common in web design and other types of coding projects.


1 Answers

With questions like this, I'd recommend reading the spec, especially the Grammar section. Syntax like < something > is used in

  1. Type Parameters

    • Defined as < TypeParameterList > in section 3.6.1
    • Used with declarations and call signatures of classes, interfaces, functions and more

      function heat<T>(food: T): T { return food; }           //^^^^^ Type parameter list  class Pizza<T, E extends Cheese> { toppingA: T; toppingB: E }          //^^^^^^^^^^^^^^^^^^^^ Type parameter list 
  2. Type Arguments

    • Defined as < TypeArgumentList > in section 3.6.2
    • Used with references to generic types and calls to generic functions

      var pizza: Pizza<Pepperoni, Mozzarella>;                //^^^^^^^^^^^^^^^^^^^^^^ Type argument list pizza = heat<{ toppingA: Pepperoni, toppingB: Mozzarella}>(ingredients)             //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type argument list 

      Update 2018-07-01: As of version 2.9, generic type arguments can also be used in JSX elements and tagged templates.

       <MenuItem<Pizza> toppings={[Pepperoni, Mozzarella]} />         //^^^^^^^ Type argument list   const ratingHtml = escapeUserInput<string | number> `Customer ${feedback.customer.username} rated this pizza with <b>${feedback.rating}</b>/10!`                                   //^^^^^^^^^^^^^^^^ Type argument list 
  3. Type Assertions

    • Defined and used as < Type > UnaryExpression where UnaryExpression comes from EcmaScript standard in section 4.16

      var ingredients = {     toppingA: new Pepperoni,     toppingB: <Mozzarella> fridge.takeSomeCheese()             //^^^^^^^^^^^^ Type assertion }; 
  4. JSX expressions (when enabled)

    • Not documented in the spec, but should follow the the syntax of JSX, which is basically an expression like

      <JSXElementName JSXAttributes(optional)> JSXChildren(optional) </JSXElementName> 

      or

      <JSXElementName JSXAttributes(optional) /> 
like image 200
noppa Avatar answered Sep 24 '22 00:09

noppa