Chapter 2 Introduction to TypeScript
1.
var brand: string = 'Chevrolet';
var message: string = `Today it's a happy day! I just bought a new ${brand} car`;
no more var; use the let keyword wherever possible.
The const keyword is a way to indicate that a variable should never change.
const brand: string[] = ['Chevrolet', 'Ford', 'General Motors'];
The null and undefined literals require special treatment. In a nutshell, they are typed under the any type,
which makes it possible to assign these literals to any other variable, regardless of its original type.
2. custom types
type Animal = 'Cheetah' | 'Lion';
const animal: Animal = 'Cheetah';
enum Brands { Chevrolet, Cadillac, Ford, Buick, Chrysler, Dodge };
const myCar: Brands = Brands.Cadillac;
3. fat arrow functions
const double = x => x * 2;
const add = (x, y) => x + y;
const addAndDouble = (x, y) => {
const sum = x + y;
return sum * 2;
}
4.
Class decorators is executed before the class gets instantiated.
No comments:
Post a Comment