Chapter 2 TypeScript
1.
npm install -g typescript
npm install -g tsun (TypeScript Upgraded Node)
tsun
> var fullName: string = 'Nate Murray'
> console.log(fullName)
var jobs: Array<string> = ['IBM', 'Microsoft', 'Google']; equals with the below
var jobs: string[] = ['Apple', 'Dell', 'HP'];
enum Role {Employee, Manager, Admin};
var role: Role = Role.Employee;
console.log('Roles: ', Role[0], ',', Role[1], 'and', Role[2]);
any is the default type if we omit typing for a given variable. Having a variable of
type any allows it to receive any kind of value.
var something: any = 'as string';
something = 1;
something = [1, 2, 3];
2.
//ES5-like example
var data = ['Alice Green', 'Paul Pfifer', 'Louis Blakenship'];
data.forEach(function(line) { console.log(line); });
However with the fat arrow => syntax we can instead rewrite it like so:
// Typescript example
var data: string[] = ['Alice Green', 'Paul Pfifer', 'Louis Blakenship'];
data.forEach( (line) => console.log(line) );
Parentheses are optional when there’s only one parameter.
data.forEach( line => console.log(line) );
3.
var firstName = "Nate";
var lastName = "Murray";
// interpolate a string (enclose your string in backticks)
var greeting = `Hello ${firstName} ${lastName}`;
console.log(greeting);
TODO: page 121
No comments:
Post a Comment