let x: string = 'Satendr Chauhan';
function show(val: string) {
console.log(val);
}
show(x);
let x: number = 30;
function show(val: numbre) {
console.log(val);
}
show(x)
//Boolean type
let b: boolean = true;
function showBool(val: boolean) {
console.log(val);
}
showBool(b);
// Function
function getTime(): number {
return new Date().getTime();
}
function showSome(): object | string | number {
const obj = {
name: 'saten',
};
return obj;
//return 'chauhan';
//return 45;
}
console.log(showSome());
function showMsg(msg: string): void {
console.log(msg);
}
showMsg('This saten Pratap')
let arr: string[] = ['php', 'java', 'React', 'Javascript']; // String array type
let array: [string, number, boolean] = ['saten', 45, true]; // Specific value in array type
let arry: any[] = ['saten', 45, true]; // Array type but value can be anything string, number, boolean etc.
// Using sqaure brackets to creat array
const nums: number[] = [13, 4, 8, 2, 6, 10, 12]; //Type inference, number array type
const data: number[] = [4, 5, 6, 7, 8, 2, 3, 1];
//Using array constructor to create array
const arr: number[] = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9);
//Using the Array.of method to create array
// const names: string[] = Array.of('Ram', 'Sham', 'Naman', 'Tapan');
// Array with multiple data types
const arrData: (string | number | boolean)[] = [
'saten',
36,
'kanpur',
true,
'up',
20,
];
console.log(arrData);
let data:unkow = 12;
data="Satendr" // string given
// Casting is override a type using as
let city: unknown = 'Mumbai';
console.log((city as string).length);
function showError(errorMsg:string):never {
throw new Error(errorMsg);
}
let a: any = 'Saten Chauhan';
let arry: any[] = ['saten', 45, true];
/* BigInt */
let bigintNum = Number.MAX_SAFE_INTEGER;
let num1: bigint = 9007199254740991n;
let num2 = BigInt('9007199254740991');
console.log(num2);
interface RecObj {
height: number,
width: number
}
const rectangle: RecObj = {
height: 40,
width: 20
};
let xname: string | number | boolean = 'saten';
xname = 'chauhan';
xname = 36;
xname = true;
// console.log(xname);
let aname: string = 'Chauhan';
let bnum: number = 40;
let cname: boolean = true;
// let xyz: string | number | boolean = aname | bnum | cname;
const showValue = (
value: string | number | boolean
): string | number | boolean => {
console.log(value);
return value;
};
showValue('I am a mobile and web app developer');
showValue(786);
showValue(true);
const showData = (
value: string,
padding: number | number
): string | number | boolean => {
console.log(value);
return value + padding;
};
showData('I am from Ayodhya, Uttar Pradesh', 30);
// Defining tuples type for user information
type UserInfo = [string, number, string, string, string, boolean];
// Defining tuples type for user information with readonly which mean we can update data structure in tuples
type PersonInfo = readonly [string, number, string, string, string, boolean];
const userData: UserInfo = [
'saten',
36,
'Kanpur',
'Uttar Pradesh',
'India',
true,
];
userData.push('Raman');
console.log(userData);
const displayInfo = (user: UserInfo): void => {
const [name, age, city, state, country, drivinLicense] = user;
console.log(
`I am ${name}, ${age}, form ${city}, ${state}, ${country}, having driving license ${drivinLicense}`
);
};
displayInfo(userData);
function show(value: T): T {
return value;
}
const result1 = show(60);
const result2 = show('Pratap');
const result3 = show(true);
// console.log(result2);
function showResult(name: T, city: S) {
return typeof name == 'number' ? name + +city : `${name}, ${city}`;
}
const r1 = showResult('Singh', 120);
const r2 = showResult('Singh', 'Chauhan');
const r3 = showResult(40, 120);
const r4 = showResult('isAdmin', true);
// console.log(r1);
// console.log(r2);
// console.log(r3);
// console.log(r4);
// console.log(data1.name);
interface DemoData {
name: S;
city: S;
age: T;
isAdmin: B;
}
const myData: DemoData = {
name: 'Saten Singh',
city: 'Hyderabad',
age: 36,
isAdmin: true,
};
function getData(user: DemoData) {
console.log(user);
}
getData(myData);
/* typesof */
let s: string = 'saten';
let n: number = 12;
let und: undefined = undefined;
let b: boolean = true;
const f = () => 'Hello world';
let a: number[] = [1, 2, 3, 4];
typeof s === 'string'; // string
typeof n === 'number'; //number
typeof b === 'boolean'; // boolean
typeof undefined === 'undefined'; //undefined
typeof f === 'function'; //function
Array.isArray(a); // array
//Type inferences
const name = 'saten'; //Automatically will declare and assign type to object
type Students = {
name: string;
age: number;
gender?: string;
greet: (country: string) => string; // Call Signature
};
const student: Student = {
name: 'saten',
age: 36,
greet: (country): string =>
`I am ${student1.name}, and I am ${student1.age} years old, from ${country}`,
};
parameter_name? : return_type;