string:let car: string = "ford";car = "citroen";Template strings:
span multiple lines
surrounded by backquote characters `
can embed exressions into it
let car: string = "Veyron Super Sport";let speed: number = 268;let phrase = `${car} is recognized as the world’s fastest production car by Guinness. It's top speed is ${speed} mph. Would you like to drive it?`;alert(phrase);
enums:enum make { Audi, AlphaRomeo, BMW, Citroen, Ford, Holden, Hyundai, Jaguar, Kia };let myCarMake = make.Ford;alert(myCarMake);any:let age: any = "five";age = 5;
Diapositiva 5
Basic Types: void, null, undefined, never
void:
no type at all
used in functions that do not return value
if variable is declared of type void, can only assign null or undefined
null and undefined:
they are subtypes of all other types
if --strictNullChecks is on, can only assign null and undefined to void
never:
type that never occurs
return type of the function that never returns (unreachable end point)
Diapositiva 6
Basic Types: Type Assertions
Type Assertions:let someonesName: any = "John";// Angle Bracket syntaxlet nameLength: number = (<string> someonesName).length;//"as" syntax let nameLength2: number = (someonesName as string).length;
not as strict as in c#
structural subtyping
object has to have all the same properties as interface
does not have to be derived or created as the same interface
let circle = new Shape("red", "circle");alert(circle.color);
Diapositiva 20
Abstract classes
abstract class Shape { abstract getColor(): string; color: string;}
Diapositiva 21
Functions
function multiply(x: number, y: number) :number { return x * y;}// Anonymous functionlet times = function (x: number, y: number): number{ return x * y; };let times2: (x: number, y: number)=>number = function(x: number, z: number): number { return x * z; };
function multiply3(x: number, y: number, z?: number): number { if (z !== null && z !== undefined) { return x * y * z; } else { return x * y; } }function multiply4(x: number, y: number, z: number = 1): number { return x * y * z;}
Diapositiva 22
Functions
function multiply(a: number, b: number, ...xyz: number[]){ let product = a * b; for (let c of xyz) { product = product * c; } return product;}