let name: string; // stores text
let age: number; // stores numbers
let isMale: boolean; // stores a true or false values
let typeAny: any; // stores any type of value
let typeNull: null = null; // can only store a null value
let typeUndefined: undefined = undefined; // can only store undefined value
// these are the basic types of variables in TypeScript
// and of course you can change let to const
// cannot use object for type defination because this is not recommended
// use Record<string, any> 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<string> = ["sunday","monday","thuesday","wenesday"]
const numb2: Array<number> = [1,2,3,4,5]
const person: Record<string, any> = {
name: "john doe",
age: 30
}
async function name(): Promise<string> {
return "john doe"
}
name().then(console.log)
async function str(): Promise<string[]> {
return ["sunday","monday","thuesday","wenesday"]
}
str().then(console.log)
async function int(): Promise<int[]> {
return [1,2,3,4,5]
}
int().then(console.log)
async function objectValue(): Promise<Record<string, any>> {
const person: Record<string, any> = {
name: "john doe",
age: 30
}
return person
}
objectValue().then(console.log)
async function objectValueMulti(): Promise<Record<string, any>[]> {
const person: Record<string, any>[] = [{
name: "john doe",
age: 30
},{
name: "jane doe",
age: 30
}]
return person
}
objectValueMulti().then(console.log)
function identity<Type>(arg: Type): Type {
return arg;
}
let fun = identity<string>("hello world");
console.log(fun);
/*think of this as you can specify the type later*/
/*Directly specifying version of the above would be something like
function identity(arg: string): string {
return arg;
}
let fun = identity("hello world");
console.log(fun);
*/