week 9.3, enum, generics, import, export, typescript.
//enum is another cleaner way to write constant value
enum direction {
Up,
Down,
Left,
Right
}
Up,
Down,
Left,
Right
}
doSommthing(direction.up)
const app = express();
enum ResponseStatus{
Success = 200,
NotFond = 404,
Error = 500
}
//200, 404, 500
app.get("/", (res, res) => {
if (!req.query.userId) {
res.status{ResponseStatus.NotFond}
}
//and so on
res.json({});
})
app.get("/", (res, res) => {
if (!req.query.userId) {
res.status{ResponseStatus.NotFond}
}
//and so on
res.json({});
})
//Generic
// User can send different type of values in inputs, without any type errors.
// Typescript isn't able to infer the right type of the return type
// User can send different type of values in inputs, without any type errors.
// Typescript isn't able to infer the right type of the return type
// ugly
type Input = number | string;
//200, 404, 500
function firstEl(arr:Input[]) {
return arr[0];
}
const value = firstEl([1,2,3, "dhrup"])
//-------------------------
//pretty
//<T> represent any thing value is pass on the parameter.
//it is a generics
function identity<T>(arg: T) {
return arg;
}
let output1 = identity("myString");
let output2 = identity(100);
//-----------------------
function getFirstElement<T>(arr: T[]) {
return arr[];
}
var output1 = getFirstElement(["harkiratSingh", "dhrupSinha"]);
var output2 = getFirstElement([100,30]);
console.log(output1.toLowerCase());
//importing in TS
import express from "express";
//exporting in TS
export default a;
export const a = 1;
Comments