Primitive Types
Before we dig into Typescript specific types let's look at some of the primitive types that we already know from JavaScript and can also use in Typescript.
string
, number
, and boolean
JavaScript has three very commonly used primitives: string
, number
, and boolean
. Each has a corresponding type in TypeScript.
As you might expect, these are the same names you’d see if you used the JavaScript typeof
operator on a value of those types:
string
represents string values like"Hello, world"
number
is for numbers like42
. JavaScript does not have a special runtime value for integers, so there’s no equivalent toint
orfloat
- everything is simplynumber
boolean
is for the two valuestrue
andfalse
To declare the type for a variable or function we append it to the name when initializing by using a semi-colon :
followed by the type.
ts
// ExamplesletfullName : string = "John Doe";letage : number = 43;letisAdult : boolean = true;console .log (typeoffullName ); // stringconsole .log (typeofage ); // numberconsole .log (typeofisAdult ); // boolean
Next, Let look at some more primitive types that we already know from JasvaScript