# TypeScript | | **TypeScript** is a free and open-source high-level programming language developed by Microsoft that adds static typing with optional type annotations to JavaScript. It is designed for the development of large applications and transpiles to JavaScript. Because TypeScript is a superset of JavaScript, all JavaScript programs are syntactically valid TypeScript, but they can fail to type-check for safety reasons. | |-|-| | | wikipedia:: [TypeScript](https://en.wikipedia.org/wiki/TypeScript) | url:: [TypeScript](https://www.typescriptlang.org) [[JavaScript]] - Makes the code completion work better - A superset of JavaScript. This means that you can write JavaScript code and use - it in TypeScript files. - Statically typed ## Meta - [What is TSDoc? | TSDoc](https://tsdoc.org/?cmdf=TSDoc) ## Variables - let variableName: type = value; - let variableName: number = 10; - let variableName: string = “10”; - let variableName: boolean = true; ## Data Structures ### Arrays - Arrays in TypeScript must contain values of the same type. If you need different types, you can use a tuple - let arrayName: number\[\] = \[10,9,8\]; ### Tuples - A tuple is a special structure in TypeScript that can hold as many values as needed of different types. - let tupleName: \[number, string, number\]; tupleName = \[10, “9”, 8\]; - Is it mutable? ## Functions - Named Functions - function myFunction(x: number): number { return x*2; } - function myFunction(x: number): void { y = x*2; } - Anonymous Functions - let myFunction = function(x: number): number { return x\*2; }; - Optional Parameters - In JavaScript, you can declare a function with 5 parameters and only give it 2 when it comes time to use it. In TypeScript, that is not the case. In TypeScript when you declare a function with 5 parameters, you have to give the function 5 arguments when calling it, unless you make some of those parameters optional. This means that when you are calling the function, you can leave off the optional parameter(s). To denote a parameter as optional, you can use ? notation. Any parameters that are optional must follow the required parameters. - function myFunction(a: number, b?:number): number { return a+b+5; } console.log(myFunction(1,2)); console.log(myFunction(1)); - Default Values - function myFunction(a: number, b = 5): number { return a+b+5; } console.log(myFunction(1,2)); console.log(myFunction(1)); ## Classes - class Astronaut { name: string; constructor(firstName: string, lastName: string) { this.name = firstName + ” ” + lastName; } greet() { return “Hello, ” + this.name; } } let Bob = new Astronaut(”Bob”,”Smith”); - You can also use inheritance with extends in the normal way - Use optional Properties instead of null ### Interfaces ## Types - You can solve the potential for undefined in e.g. `process.env.someVar` by putting an ! at the end which makes it assert it as never undefined or something? `process.env.someVar!` ## Sources - [28. TypeScript — Introduction to Professional Web Development in JavaScript documentation](https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/index.html)