TypeScript overview
checking the validity of JavaScript types with this library
2020-10-01 12:00
// updated 2025-04-25 16:06
// updated 2025-04-25 16:06
TypeScript extends JavaScript to ensure that a variable's data conforms to a certain form, or "type"!
Why TypeScript?
TypeScript allows for easier debugging and prevents bad data from circulating through an app:
- also, all JavaScript works in TypeScript
- however, TypeScript must transpile into JavaScript to work on the web
The essence of TypeScript
We use TypeScript to validate variables by assigning each variable in TypeScript a type:
1function myFunction(
2 isYearRound: boolean,
3 cost: number,
4 destination: { name: string }
5) {
6 ...
7}
8Breaking that down:
- the
isYearRoundvariable has a primitive type ofboolean- thus,
isYearRoundcan only be eithertrueorfalse
- thus,
- the
costhas a type of anumber- (JavaScript has no integers or floats; all numeric data are just
numbers) - thus,
costcannot have symbols other than digits and legal operators
- (JavaScript has no integers or floats; all numeric data are just
- the
destinationhas the type of an object{}- this object, in turn, contains a property called
name- the
namehas a type ofstring
- the
- this object, in turn, contains a property called
Abstracting that further:
1function myFunction(
2 propertyX: type,
3 ...
4) {
5 ...
6}typecan take on any of the following (with a few more others):stringnumberbooleanDateunknownvoidnever
- the
typecan also be arrays of each of the above, e.g.string[]number[]
- as shown in the earlier top snippet, the
typecan also be an object- the properties in that object would also get their own
types
- the properties in that object would also get their own