You are on page 1of 1

15 

Typescript Quick 
Reference 
Types  Functions 
let 
 b
 etter    than   
var   m
 ore   scope    
and   call    
strict.   function   
add(x: number ,  
y: number ): number   { r
 eturn    
x  +  
y  

Use 
 c
 onst   f
 or   variables    
and    
readonly   f or   
properties  let 
  
myAdd:(x: number  ,y: 
number )=> number    
=  add;   // 
  
function    
type 
typeof:   
like    
javascript    
so:   l
 et   
x: number  ; t ypeof    
x  ==   
"number"  function   
example(defval:  string ="def",    
optionalval  ?:
 s
 tring ){…} 
type 
  alias:   t
 ype   Custom    
= s tring  ;  function   
params(fn:  string , .
 .. rest: string[]  )  {…} 
boolean:  l
 et    
isDone:   b
 oolean   =
   false;  Arrow   
function   
captures    
this   
where   
function    is 
  
created: 
number:   l
 et    
value:   n umber    
=  6;  (
 or   0xf00d,    
0b1010,    
0o744)  let 
  
something   =  
{  exampleFunc:   f unction  () 
  
{  
string:   l
 et    
name:   s
 tring    =  "Something\n"    +  
fname    +  (age   
+  
1);      r
 eturn   
() 
  
=>   
{…}  /
 /   
stuff   
using   ̀this`    
}  }; 
array<>:  l
 et    
list:   n
 umber[]    =  [1,2,3];    
         l  et   
list2:   A rray<number>    
=  [1,   2,   
3];  Generics 
tuple:     let   
x:  [
 string,    number]  ;  x  =  ["hello",    
10];  function   
exFunc<T>(arg:T,    aarg:T[],    
aaarg: Array  <T>):T 
  
{…} 
enum: 
    e  num   
Color    {Red,    
Green};   l
 et    
c:   
Color    =  Color.Green;  let 
  
myExFunc:<T>(arg:T,    
aarg:T[],    aaarg: Array <T>)=>T   
=  exFunc; 
         e  num   
Test    
{  V1=1,V2="123".length};    Test[Test.V1]==1;  class 
  
GenericExample<T>    
{  value:    
T;   

any: 
     l  et   
n:  a
 ny   =  4; 
  
n   =  
"str";    
n  =  false;   l et   
an:  a
 ny[] ;  let 
  
c  
= n ew   
GenericExample<  string  >(); 
void: 
    f  unction    test():   v
 oid   {…}  Setting   
up   
a  generic   
constraint: 
special:  u
 ndefined  ; n ull ;  interface   
StuffWithLength    {  length:   n
 umber ;  

never:    f unction    err(msg:  string  ): never  {t hrow    
new   Error(msg);}  function   
exFunc2<T   e
 xtends    
StuffWithLength>(arg:T):T    
{…} 
type 
  assertions:   l et   
s: number  =(< string  >strval).length;    
//casts  For 
  
factory,    necessary   to   
refer    
to   
class   
type   
by   
constructor: 
to 
  
directly    
cast:    
something    
=  other   a
 s   
type;  function   
create<T>(c:    
{n
 ew  ():   
T;}):T    
{ r
 eturn    
new   
c(); 
  

   
Destructuring 
  
Array/Object  Iterators 
swapping:   [first,   second]   
=  [second,   
first];  for 
(let 
  
i  
in 
  
list) 
  { r
 eturns 
  
keys 
  
"0",   
"1", 
  
"2", 
  
.. 
  

for 
  
params:   for 
(let 
  
i  
of 
 l
 ist) 
  
{ r eturns 
  
values 
  

    f
 unction    
f([first,second]:[number,number])    
{…}   
    l
 et   
[first,    
...rest]    
=  [1, 
  
2,   
3]; 
//first=1,rest=[2,3]  Modules 
  and 
  Namespaces 
    l
 et  [
 ,   
s, 
  ,  
f]   
=  
[1,2,3,4];  //s=2,f=4,    
rest 
  
is 
  
omitted  Each 
  
typescript    runs   in   
own   
scope.   e
 xport   v
 ars,    funcs,    
classes, 
Same 
  for 
  objects   gives   
multiple    
useful   
features.    interfaces,..    and  i mport   t
 hem    
in   
another    script    to 
  use. 
  export 
 i
 nterface    IExample    
{…} 
Interfaces  export 
 c
 onst    
someregex    =  
/^[0‐9]+$/; 
interface    Example    
{   export 
 c
 lass    
CExample   i
 mplements    
CParent    
{…}   /
 /module_name.ts 
     label:   s
 tring  ; / / 
  mandatory    property  export 
  
{  CExample   a s 
  RenamedExportExample    
}; 
     color?:   s tring  ; / /   
optional    property  from 
  
other   files,    you   
can: 
     [propName:   s
 tring  ]:  a ny ; /
 /   
could    
have    
any   number    
of   
props  export   
{CExample   a
 s   
AReExport}   f rom   
"./module_name";   /
 /reexport 
     (par1:   s
 tring  ,  par2:   s
 tring  ):  b
 oolean  ;  //func    
signature  export 
  
*  from   "./module_name";   / / 
  exports    
class    CExample 
     [index:   n umber  ]: string  ; / /   
class    
can   
be   indexed    
into  To 
  
import   
from    
another    module: 
}  import 
  
{CExample}   f rom   "./module_name";   l
 et   m  = n ew 
  CExample(); 
class   Clock  i mplements    
ClockInterface    
{…}  import 
  
{CExample   a
 s   
CMy}   f
 rom   "./module_name";   /
 /   
rename 
interface    ExampleExtend   e xtends    
Example,    
ExampleOther    
{…}  import 
 *
  a s 
  EX  f
 rom    
"./module_name";   l et   
m  = n ew   EX.CExample(); 
  A 
  
unique   
default    
exports    
file    
can   be   
used:    module.d.ts 
Classes  declare 
 l
 et   $:   
JQuery;   e xport  d efault    
$; 
members    
are  p ublic    by 
  default.    
can   be 
  individually    
set   to  Then 
  
from   
another    module    
to   
import    
that    
stuff: 
private    
or  p
 rotected  .  use  r
 eadonly   f
 or   constants.  import 
  
$ f rom   
"JQuery;    $("something").html("something"); 
class   Example    
{  Classes 
  and   
funcs    can   
be   
authored    directly    as 
  default    exports: 
     prop1:   s
 tring  ;  export 
 d
 efault   c lass    
CExample    {…} 
    s tatic    
stprop:    
{x:0,    
y:0};  From 
  
another    
module: 
    c onstructor  (msg:   s
 tring  )  
{  this.prop1    =  msg;   
}  import 
  
Whatever   f rom   
"./module_name";   /
 /   
Whatever    
==   
CExample 
     method()    
{…}  For 
  
standard    
require    functionality,    to 
  export    then   import: 
    g et   
prop1_accessor():   s tring   { r eturn   this.prop1;    }  export 
  
=  CExample; 
    s et   
prop1_accessor(s:  string  )  {  this.prop1    
=  s; 
  }  import 
  
ex   
= r equire  ("./module_name"); 
}  Namespaces   are   
useful    
when    
working    
with    
modules: 
let   
exclass    = n
 ew    
Example("Hello!");  namespace   
NExample    { e xport   i
 nterface    
IExample    
{…}   …  

class   ExampleInherit   e
 xtends    
Example    
{  To 
  
use 
  
same    
namespace    
for   N  
modules    use   
special    comment    before: 
    c onstructor  (msg:   s
 tring  )  
{ s uper  (msg);    
}  /// 
  
<reference    
path="module_name.ts"    
/> 
     move(dist    =  5) 
  { s uper  .move(dist);    
}  Aliasing   
namespaces: 
}  import 
  
ex   
=  NExample.CExample; 
abstract    class   Test    
{  
    a bstract    func1():   v oid ; 
     func2():   v
 oid    
{…} 

 / / 
  Abstracts    
can   be   
extended    
by   
classes    
of   
course 

You might also like