Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedef just like C/C++

Tags:

typescript

I've been using Basarats's excellent Collections library slightly updated for 0.9.0 creating types such as:

Dictionary<ControlEventType,      Dictionary<number, (sender: IControl,                          eventType: ControlEventType,                          order: ControlEventOrder,                          data: any) => void >> 

Now I don't want to have to write this in full every time I use it. One of the approaches which seems to work is:

export class MapEventType2Handler extends C.Dictionary<ControlEventType,                                                 C.Dictionary<number,                                                 (sender: IControl,                                                  eventType: ControlEventType,                                                  order: ControlEventOrder,                                                  data: any) => void >> {} 

I can then write:

EH2: MapEventType2Handler = new MapEventType2Handler(); 

instead of:

EH: Dictionary<ControlEventType,          Dictionary<number,          (sender: IControl,           eventType: ControlEventType,           order: ControlEventOrder,           data: any) => void >>; 

Anyone come across any better ideas?

I am also experimenting with 'typedeffing' various function signatures without great results.

like image 266
andyks Avatar asked Jun 18 '13 08:06

andyks


People also ask

Should I use typedef in C?

typedef'ing structs is one of the greatest abuses of C, and has no place in well-written code. typedef is useful for de-obfuscating convoluted function pointer types and really serves no other useful purpose.

What is a typedef in C?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

Is typedef same as define?

typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc. typedef interpretation is performed by the compiler where #define statements are performed by preprocessor.

Is typedef the same as using?

In C++, 'using' and 'typedef' performs the same task of declaring the type alias. There is no major difference between the two. 'Using' in C++ is considered to define the type synonyms. This method is also known as alias- declaration.


2 Answers

From version 1.4 Typescript supports type aliases (source, see also this answer):

type MapEventType2Handler = Dictionary<ControlEventType,      Dictionary<number,      (sender: IControl,       eventType: ControlEventType,       order: ControlEventOrder,       data: any) => void >>; 
like image 52
splintor Avatar answered Oct 15 '22 17:10

splintor


First off thanks for the kind words :).

Your solution is actually optimal.

Long answer Typescript has two Declaration spaces. Types and Variables.

The only way to introduce items into the type declaration space is via a class or an interface ( 0.8.x could use module to introduce a type as well. Its removed from 0.9.x)

Interface will not work since you want the implementation to stay intact (and interface is implementation independent).

Variables will not work since they do not introduce a name in the Type Declaration space. They only introduce a name in the variable declaration space.

e.g.:

class Foo {     }  // Valid since a class introduces a Type AND and Variable var bar = Foo;   // Invalid since var introduces only a variable so bar cannot be used as a type // Error: Could not find symbol. Since compiler searched the type declaration space  var baz: bar;   // Valid for obvious reasons  var x: Foo;  

What you want could be done if the language had a macros but for now class+extends is the only approach.

like image 23
basarat Avatar answered Oct 15 '22 16:10

basarat