typescript type declaration code example

Example 1: typescript function type

// define your parameter's type inside the parenthesis
// define your return type after the parenthesis

function sayHello(name: string): string  {
  console.log(`Hello, ${name}`!);
}

sayHello('Bob'); // Hello, Bob!

Example 2: typescript function type

interface Date {
  toString(): string;
  setTime(time: number): number;
  // ...
}

Example 3: typescript type declaration

// cannot use object for type defination because this is not recommended
// use Record this same with object

const name: string = "john doe"
const age: number = 30

const days1: string[] = ["sunday","monday","thuesday","wenesday"]
const numb1: number[] = [1,2,3,4,5]

const days2: Array = ["sunday","monday","thuesday","wenesday"]
const numb2: Array = [1,2,3,4,5]

const person: Record = {
	name: "john doe",
	age: 30
}

async function name(): Promise {
	return "john doe"
}

name().then(console.log)

async function str(): Promise {
	return ["sunday","monday","thuesday","wenesday"]
}

str().then(console.log)

async function int(): Promise {
	return [1,2,3,4,5]
}

int().then(console.log)

async function objectValue(): Promise> {
	const person: Record = {
	 name: "john doe",
	 age: 30
	}
  return person
}

objectValue().then(console.log)

async function objectValueMulti(): Promise[]> {
	const person: Record[] = [{
	 name: "john doe",
	 age: 30
	},{
	 name: "jane doe",
	 age: 30
	}]
  return person
}

objectValueMulti().then(console.log)

Tags:

Misc Example