Typescript introduction and setup

Created By: chatGPT

TypeScript is a superset of JavaScript that adds static typing. It helps developers catch errors during development rather than at runtime. In this tutorial, you will learn how to set up TypeScript in your project and understand its basic features.
To start using TypeScript, first ensure that you have Node.js installed. You can check this by running the following command in your terminal:
node -v
If Node.js is installed, you can set up TypeScript using npm, which is included with Node.js. To install TypeScript globally, run:
npm install -g typescript
After installation, you can verify that TypeScript is installed correctly by checking its version:
tsc -v
Now, create a new directory for your project and navigate into it:
mkdir my-typescript-project
cd my-typescript-project
Initialize a new Node.js project with a package.json file:
npm init -y
Next, create a tsconfig.json file in the root of your project. This file is used to configure the behavior of TypeScript. You can generate a default configuration file with the following command:
tsc --init
Open the tsconfig.json file and modify it according to your preferences. For example, you can set the target JavaScript version and enable strict mode:
{
    "compilerOptions": {
        "target": "es6",
        "strict": true
    }
}
Now, create a new TypeScript file to start coding. Create a file named index.ts in your project root:
touch index.ts
In index.ts, you can write your first TypeScript code. Here’s a simple example where we define a function that adds two numbers:
function add(x: number, y: number): number {
    return x + y;
}

console.log(add(5, 3));
To compile your TypeScript code to JavaScript, use the TypeScript compiler (tsc). Run the following command:
tsc
This command will create an index.js file in the same directory. You can then run the generated JavaScript file using Node.js:
node index.js
Congratulations! You have set up TypeScript and run your first program. TypeScript not only helps you catch errors early but also provides features like interfaces, enums, and modules to organize and strengthen your code.
Introduction And SetupVariablesData TypesAnyUnknownVoidNeverStringNumberBooleanArrayTupleEnumObjectInterfaceType AliasMethodsFunctionArrow FunctionReturn TypeParametersDefault ParametersOptional ParametersRest ParametersControl FlowIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor...of LoopFor...in LoopBreak StatementContinue StatementFunctionsFunction OverloadingRecursive FunctionExpression FunctionOperatorsArithmetic OperatorsAssignment OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsConditional (ternary) OperatorData StructuresArrayTupleObjectMapSetCommentsSingle Line CommentMulti Line CommentSyntaxType AnnotationsType InferenceNamespaceModuleExportImportDecoratorsAmbient DeclarationsEvent HandlingEvent ListenersAddEventListenerRemoveEventListenerError HandlingTry...catch StatementThrow StatementFinally BlockCustom Error TypesAsync AwaitPromisesGenericsConditional TypesMapped TypesIntersection TypesUnion TypesLiteral TypesDiscriminated UnionsType GuardsTypeof GuardInstanceof GuardAssertion FunctionsModule Augmentation