TypeScript Basics

  • string : A sequence of characters
  • number: Numbers less than bigint
  • bigint: Numbers larger than 2⁵³ — 1
  • boolean: True/False
  • undefined: undefined
  • symbol: Symbol(‘*’)
  • null: null
Any type
function errorLogger(complexRemoteObj: any) {
...
}
Void type
const logError(errors: string []) :void => {
for(let error of errors) {
console.log(error)
}
}
Unknown type
const emailError(error1: unknown, error2: unknown) {
if(typeof error1 === 'server' && typeof error2 === 'data') {
...sending email
}
}
Try it out here
Common Declarationsvariable
const hometown: string = 'NYC';
array
const numberArray: Array<number> = [1,2,3];
or
const stringArray: string[] = ['a','b','c'];
tuple
const pet: [string, number] = ["Toby", 27];
object
const pet: {name: string; age: number;} = {
name: 'Toby';
age: 27;
}
Try it out here
Inline Function DeclarationsRegular function
function greetNthTimes(greeting: string, xTimes: number) : string {
let str = ''
for(let i = 0 ; i <= xTimes; i ++) {
str += `${greeting} \n`
}
return str
}
console.log(greetNthTimes('hi', 10))
Arrow function
const greetNthTimes = (greeting: string, xTimes: number) : string => {
let str = ''
for(let i = 0 ; i <= xTimes; i ++) {
str += `${greeting} \n`
}
return str
}
console.log(greetNthTimes('hi', 10))
Try it out here
type Person = {
age: number;
name: string;
email?: string;
}
Try it out here
Interface Person {
age: number;
name: string;
email?: string;
}
Try it out here
type FavoriteChar: string | number | Symbol | null
type Borough: "Queens" | "Brooklyn" | "Staten Island" | "Bronx" | "Manhattan" 
type Name {
name: string;
}
type Age {
age: number
}
type Breed {
breed: string
}
type Pet = Name & Age & Breed
const toby: Pet {
name: 'toby';
age: 27;
breed: 'lab';
}
Try it out here
Array Generic Type 
const stringArray: Array<string> = ['hi','hello','i']
const numberArray: Array<number> = [1,2,3]
Regular Generic function 
function logChar<ItemType1>(char: ItemType1, x: number) :void {
for(let i = 0 ; i <= x; i++) {
console.log(char)
}
}
console.log(logChar<string>('howdy!', 10))
Arrow Generic function
const logNum = <ItemType1,>(char: ItemType1, x: number) => {
for (let i = 0; i <= x; i++) {
console.log(char);
};
}
Try it out here
interface Form<Type1> {
value: Type1;
}
type Form<Type1> {
value: Type1;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store