// this will simply log the hello world in console just like java script. console.log("hello world")// There we are specify the type of argument so that our program, knows how which kind of data is required by each aragument in a function.function home(a: string, b: number) { console.log(`sum of two number are ${ a + b }` ) }home(24,25)// Here we create a simple function without telling typescript what kind of data you need to send as an argument.
Get Revive Coding’s stories in your inbox
Join Medium for free to get updates from this writer.
gretting(24, "24 june")// Now we are creting an greeting function, which is going to deliver a welcome message and willing to change the output as loawerCase.Note: Here we also don't specify the type of data used by message, out pervious example has shown you typescript can automatically check the type of var data for simple varible based on what is our input. but in function and object we need to specify the data type, just to insure that data should be required format.var message = "Hello How are You?" console.log(message.toLowerCase()) console.log(message.toLocaleLowerCase()) message()
let user = { name: 'vikash', age: 26, location: message }
console.log(user.name) console.log(user.location)
function greet(person: string, date: Date) { console.log(`Hello ${person}, today is ${date.toDateString()}!`); }
greet("Maddison", new Date());
let obj: any = {x: 0}; console.log(obj.x)
let fname = "vikash" let lname = "singh"
function fullName (fname: string, lname: string): string { console.log( `my name is ${fname} ${lname}`) return fname } fullName(fname,lname);
function printRecord(obj: { name: string, age: number, gender: string }) { console.log(`my name is ${obj.name}`) console.log(`my age is ${obj.age}`) console.log(`my gender is ${obj.gender}`) };
function optionalRecord(obj: { name: string, age: number, gender?: string }) { console.log(`my name is ${obj.name}`) console.log(`my age is ${obj.age}`) obj.gender && console.log(`my gender is ${obj.gender}`) if (obj.gender == undefined) console.log(`gender is not not specied by user`) };
function yourId (id: string | number) { if (typeof id === 'string'){ console.log(`your string id is ${id.toUpperCase()}`) } else { console.log(`your id is ${id}`) } } yourId(25) yourId("ramuna")
function people (names: string[] | string) { if ( Array.isArray(names)){ names.forEach( function(name, index) { console.log(`Members no.${index} is ${name}`) console.log(`the first 3 character ${name.slice(0,3)} is your id `) } ) } else { console.log(`we have one member ${names}`) } } people("shayma") people(['ram','geeta','sita','lakhan','vishal'])
type point = { x: number y: number z: number }
function printCoord(pt: point) { console.log(`The coordinate's x,y,z are" (${pt.x},${pt.y},${pt.z})`); }
printCoord({ x: 100, y: 25, z: 75 });
interface poi { x: number y: number z: number }
function printCoord(pt: poi) { console.log(`The coordinate's x,y,z are" (${pt.x},${pt.y},${pt.z})`); }
printCoord({ x: 100, y: 25, z: 75 });
const myCanvas = document.getElementById("main_canvas") as HTMLCanvasElement; const myCanvas = <HTMLCanvasElement>document.getElementById("main_canvas");
const x = 'hello' as number; const y = ("25415" as any) as number const z = (y as any) as number console.log(z) console.log(typeof y) console.log(typeof z) Create function accept only a limited set of values function typeOfGroup(id: number, groupType: 'public' | 'private' | 'confidencial' ): string { console.warn(`congratulation you had sucessfully join our ${groupType} group, id is ${id}`) return groupType; } typeOfGroup(124, 'private') typeOfGroup(174, 'public')
function compair(a: string, b: string): -1 | 0 | 1 { console.log((a === b) ? 0 : a > b ? 1 : -1) return (a === b) ? 0 : a > b ? -1 : 1 } compair('514','514') compair('514','121545') compair('514121','121545') type person = { name: string }function paymentDone (nam: person | 'other') { console.log(`welcome ${nam} to our advance payment gateway`) }
paymentDone({name: 'rajat singh'}) paymentDone('other') paymentDone('metoo')Learn Web Development With a Practical Approach: Powered By :High Income Coding