29
loading...
This website collects cookies to deliver better user experience
Compiling means that source code is transformed from a human-readable format to a machine-readable format, whereas transpiling is transforming source code from one human-readable format to another human-readable format.
const birthdayGreeter = (name: string, age: number): string => {
return `Happy birthday ${name}, you are now ${age} years old!`;
};
const birthdayHero = "Jane User";
const age = 22;
console.log(birthdayGreeter(birthdayHero, 22));
name
and age
. We assign name
to the type string age
to the type numberconst birthdayGreeter = (name: string, age: number): string => { };
Typescript would yield an error if we passed in arguments of different types than ones we expect
const platform = 'freeCodeCamp';
const add = (a: number, b: number) => a + b
add
is inferred the type number.let x: someType;
let x;
npm install -g typescript
npm install --save-dev typescript
tsc --init
const myName: string = 'Joel';
const myAge: number = 99;
// myName is inferred type 'string'
const myName = 'Jonathan';
string[]
or number[]
. This effectively means 'array of strings or array of numbers'.Array<number>
or Array<string>
which means the same thing.const someValue: number | string = value;
null | undefined
can be assigned to any variable but TypeScript comes with the strictNullChecks compiler option which does not allow assigning both to a variable.function introduction(name: string, age: number): string {
return `Hello, my name is ${name} and I'm {age} years old`
}
?
operator to specify parameters that are optional. In this case, Typescript won't complain if the parameter is not passed on the function call.const introduction = (name: string, age: number, job?: string = 'developer'): string => `Hello, my name is ${name} and I'm ${age} years old. I work as a ${job}`
Any
is typically a wild card type that literally means 'whatever type'. We can also explicitly assign the type any to a variable.any
typings are usually considered to be problematic.type
keyword to define our own types.type Operator = 'multiply' | 'add' | 'divide';
Operator
type can accept either of the values. Notice how we use the OR operator |
to create a union type. In this case, any variable assigned the type Operator can accept any of the three values.type Operation = 'multiply' | 'add' | 'divide';
const calculator = (a: number, b:number, op: Operation): number => {
switch(op) {
case 'multiply':
return a * b;
case 'add':
return a + b;
case 'divide':
if (b === 0) return 'Can't divide by 0;
return a / b;
default:
return 'Operation unknow';
}
}
interface
keyword. Interfaces allow us to define the property and type of an object. An interface can have the ability to extend another interface.interface Employee {
name: string,
title: string
}
interface Manager extends Employee {
meeting: (topic: string) => void
}
name
and title
, both of which are of the type string.Manager
which has the same properties as the Employee interface but with a meeting method.const newEmployee: Employee = {
name: 'Joel',
title: 'FrontEnd Developer'
}