Basic types
Basic (primitive) types in JavaScript are String, Number, Boolean, null, undefined, and Symbol.
Types in TypeScript add couple more: any, void, and never. Let's add some types to the code!
1
let baz: any = "hello";
2
baz = 12;
Copied!
The any type is the default value for any variable or argument: this behaves exactly like JavaScript.
1
const foo: number = 12;
2
const bar: boolean = false;
3
const title: string = "Hello, Devmeetings!";
4
5
const onlyThisVerySpecificString: "specific string" = "specific string";
Copied!

Unions

The last one is quite fun: the variable can only have one particular value, everything else will throw an error. Let's make this useful:
1
const eventType: "workshop" | "boring" = "workshop";
Copied!
Now the eventType variable can only have one of the two values. This is called a union type and can be used with all types:
1
let numberOrString: number | string;
Copied!

Built-in types

There is some more complex built-in types that we can use:
1
const start: Date = new Date();
2
const e: Error = new Error("boo!");
Copied!

Arrays

It's also possible to tell TypeScript that we expect an array of some elements:
1
const numbers: number[] = [1, 2, 3]
Copied!
We can also provide types for individual elements in an array (tuple types):
1
const tuple: [string, number] = ["hello", 42]
Copied!
Tuples require an exact number of elements at exact positions.

Custom types

We can declare custom types using the type keyword:
1
type EventType = "workshop" | "boring";
Copied!
And then use them in same way as any other type:
1
const eventType: EventType = "workshop";
2
const resultTuple: [EventType, number] = ["boring", 1337]
Copied!

Resources