100% found this document useful (1 vote)
4K views1 page

Typescript Cheatsheet

The document discusses various TypeScript types such as number, string, boolean, array, tuple, and enum. It also covers TypeScript functions, including function parameters, return types, arrow functions, and generics. Examples are provided for each TypeScript feature to demonstrate how to define and use variables, parameters, and return types with the different TypeScript types.

Uploaded by

Victor Azevedo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
4K views1 page

Typescript Cheatsheet

The document discusses various TypeScript types such as number, string, boolean, array, tuple, and enum. It also covers TypeScript functions, including function parameters, return types, arrow functions, and generics. Examples are provided for each TypeScript feature to demonstrate how to define and use variables, parameters, and return types with the different TypeScript types.

Uploaded by

Victor Azevedo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 1

15

Typescript Quick
Reference
Types Functions
let
b
etter than
var m
orescope
andcall
strict. function
add(x:number,
y:number):number
{r
eturn
x+
y
}
Use
c
onst f
orvariables
and
readonly for
properties let

myAdd:(x:number ,y:
number)=>number
=add;//

function
type
typeof:
like
javascript
so: l
et
x:number ;typeof
x==
"number" function
example(defval: string="def",
optionalval ?:
s
tring){}
type
alias: t
ypeCustom
=string ; 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: number
=6;(
or0xf00d,
0b1010,
0o744) let

something=
{exampleFunc: function()

{
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: Array<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; let
an:a
ny[]; let

c
=new
GenericExample< string >();
void:
f unction test(): v
oid{} Setting
up
ageneric
constraint:
special:u
ndefined ;null; interface
StuffWithLength {length: n
umber;
}
never:function err(msg: string ):never {throw
newError(msg);} function
exFunc2<T e
xtends
StuffWithLength>(arg:T):T
{}
type
assertions: let
s:number =(<string >strval).length;
//casts For

factory, necessaryto
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)

{returns

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 runsin
own
scope. e
xport v
ars, funcs,
classes,
Same
for
objectsgives
multiple
useful
features. interfaces,.. andimport t
hem
in
another script to
use.
export
i
nterface IExample
{}
Interfaces export
c
onst
someregex =
/^[09]+$/;
interface Example
{ export
c
lass
CExample i
mplements
CParent
{} /
/module_name.ts
label: s
tring ;//
mandatory property export

{CExample as
RenamedExportExample
};
color?: string ;//
optional property from

otherfiles, you
can:
[propName: s
tring ]:any;/
/
could
have
anynumber
of
props export
{CExample a
s
AReExport} from
"./module_name"; /
/reexport
(par1: s
tring ,par2: s
tring ):b
oolean ;//func
signature export

*from"./module_name"; //
exports
class CExample
[index: number ]:string ;//
class
can
beindexed
into To

import
from
another module:
} import

{CExample} from"./module_name"; l
etm=new
CExample();
classClockimplements
ClockInterface
{} import

{CExample a
s
CMy} f
rom"./module_name"; /
/
rename
interface ExampleExtend extends
Example,
ExampleOther
{} import
*
as
EXf
rom
"./module_name"; let
m=newEX.CExample();
A

unique
default
exports
file
canbe
used: module.d.ts
Classes declare
l
et$:
JQuery; exportdefault
$;
members
arepublic by
default.
canbe
individually
setto Then

from
another module
to
import
that
stuff:
private
orp
rotected .user
eadonly f
orconstants. import

$from
"JQuery; $("something").html("something");
classExample
{ Classes
and
funcs can
be
authored directly as
default exports:
prop1: s
tring ; export
d
efault class
CExample {}
static
stprop:
{x:0,
y:0}; From

another
module:
constructor (msg: s
tring )
{this.prop1 =msg;
} import

Whatever from
"./module_name"; /
/
Whatever
==
CExample
method()
{} For

standard
require functionality, to
export thenimport:
get
prop1_accessor(): string{returnthis.prop1; } export

=CExample;
set
prop1_accessor(s: string ){this.prop1
=s;
} import

ex
=require ("./module_name");
} Namespacesare
useful
when
working
with
modules:
let
exclass =n
ew
Example("Hello!"); namespace
NExample {export i
nterface
IExample
{}
}
classExampleInherit e
xtends
Example
{ To

use

same
namespace
forN
modules use
special comment before:
constructor (msg: s
tring )
{super (msg);
} ///

<reference
path="module_name.ts"
/>
move(dist =5)
{super .move(dist);
} Aliasing
namespaces:
} import

ex
=NExample.CExample;
abstract classTest
{
abstract func1(): void;
func2(): v
oid
{}
}
//
Abstracts
canbe
extended
by
classes
of
course

You might also like