What is TypeScript?
TypeScript is JavaScript with added syntax for types.
TypeScript shares the same base syntax as JavaScript, but adds static typing.
Why TypeScript?
TypeScript allows specifying the types of data being passed around within the code, and has the ability to report errors when the types don’t match.
TypeScript uses compile time type checking. Which means it checks if the specified types match before running the code, not while running the code.
TypeScript Compiler
TypeScript is transpiled into JavaScript using a compiler.
TypeScript being converted into JavaScript means it runs anywhere that JavaScript runs!
TypeScript simple types(primitives)
There are three main primitives in JavaScript and TypeScript.
boolean
- true or false valuesnumber
- whole numbers and floating point valuesstring
- text values like "TypeScript Rocks"
There are also 2 less common primitives used in later versions of Javascript and TypeScript.
bigint
- whole numbers and floating point values, but allows larger negative and positive numbers than thenumber
type.symbol
- are used to create a globally unique identifier.
TypeScript special types
TypeScript has special types that may not refer to any specific type of data.
any
- is a type that disables type checking and effectively allows all types to be used.unknown
- is a similar, but safer alternative toany
. TypeScript will preventunknown
types from being usednever
- effectively throws an error whenever it is defined.undefined
andnull
are types that refer to the JavaScript primitivesundefined
andnull
respectively.
TypeScript Tuples
A tuple is a typed array with a pre-defined length and types for each index.
The order of value types matters for Tuples.
// define our tuple
let ourTuple: [number, boolean, string];
// initialize correctly
ourTuple = [5, false, 'Coding God was here'];