Typescript. Datatypes

// --- BOOLEAN

let b: boolean = true;
console.log('boolean ' + b + ' typeof ' + typeof b);

// --- STRING

let s: string = 'stringValue';
console.log('string ' + s + ' typeof ' + typeof s);

// --- NUMBER

let num: number = 12.24;
console.log('number ' + num + ' typeof ' + typeof num);

// --- ARRAY

let a: Array<number> = [1, 2, 3];
console.log('array ' + s + ' typeof ' + typeof a);

let a1: number[] = [1, 2, 3];
console.log('array ' + s + ' typeof ' + typeof a1);

// --- TUPLE

let t: [string, number];
t = ["hello", 20];
console.log('tuple ' + t + ' typeof ' + typeof t);

// --- ENUM

enum Color {
    Green,
    Red,
    Blue
}

let e: Color;
e = Color.Red;
console.log('enum ' + e.toString() + ' typeof ' + typeof e);
//Object.values<string>(Color).includes(Red) // check on includes

VOID, UNKKNOWN, NULL, UNDEFINED, ANY, NEVER TYPES

// --- UNKNOWN

// -- difference of unknown and any https://stackoverflow.com/questions/51439843/unknown-vs-any

let notSure: unknown = 4;
notSure = "maybe a string instead";

// OK, definitely a boolean
notSure = false;

/*declare const maybe: unknown;
// 'maybe' could be a string, object, boolean, undefined, or other types
const aNumber: number = maybe;
Type 'unknown' is not assignable to type 'number'.

if (maybe === true) {
  // TypeScript knows that maybe is a boolean now
  const aBoolean: boolean = maybe;
  // So, it cannot be a string
  const aString: string = maybe;
Type 'boolean' is not assignable to type 'string'.
}

if (typeof maybe === "string") {
  // TypeScript knows that maybe is a string
  const aString: string = maybe;
  // So, it cannot be a boolean
  const aBoolean: boolean = maybe;
Type 'string' is not assignable to type 'boolean'.
}*/

// --- ANY

let looselyTyped: any = 4;
console.log('any ' + looselyTyped + ' typeof ' + typeof looselyTyped);
// OK, ifItExists might exist at runtime
// looselyTyped.ifItExists();
// OK, toFixed exists (but the compiler doesn't check)
// looselyTyped.toFixed();

/*
let strictlyTyped: unknown = 4;
strictlyTyped.toFixed();
Object is of type 'unknown'.
*/

// --- VOID

let v: void = undefined;
console.log('void ' + v + ' typeof ' + typeof v);

/*let unusable: void = undefined;
// OK if `--strictNullChecks` is not given
unusable = null;*/

// NULL and UNDEFINED

let u: undefined = undefined;
let n: null = null;

/*By default null and undefined are subtypes of all other types. That means you can assign null and undefined to something like number.
However, when using the --strictNullChecks flag, null and undefined are only assignable to unknown, any and their respective types (the one exception being that undefined is also assignable to void). This helps avoid many common errors. In cases where you want to pass in either a string or null or undefined, you can use the union type string | null | undefined.*/

// --- NEVER
// for functions which will never end
function fNever(): never {
    while (true) {}
};

// Function returning never must not have a reachable end point
function error(message: string): never {
    throw new Error(message);
}

// Inferred return type is never
function fail() {
    return error("Something failed");
}

COMPLEX TYPES

// TYPE UNIONS

let unionExample : string | null = "123";

function printId(id: number | string) {
    console.log("Your ID is: " + id);
}
// OK
printId(101);
// OK
printId("202");

// OBJECT

/*Object
object is a type that represents the non-primitive type, i.e. anything that is not number, string, boolean, bigint, symbol, null, or undefined.*/

// The parameter's type annotation is an object type
function printCoord(pt: { x: number; y: number }) {
    console.log("The coordinate's x value is " + pt.x);
    console.log("The coordinate's y value is " + pt.y);
}
printCoord({ x: 3, y: 7 });

// --- TYPE ALIASES

type Point = {
  x: number;
  y: number;
};
 
// Exactly the same as the earlier example
function printCoord(pt: Point) {
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}
 
printCoord({ x: 100, y: 100 });

// --- INTERFACES
// --- differences from type aliases is that interfaces are extendible

interface Point {
  x: number;
  y: number;
}
 
function printCoord(pt: Point) {
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}
 
printCoord({ x: 100, y: 100 });
// Type assertions
const myCanvas = document.getElementById("main_canvas") as HTMLCanvasElement;
// or
const myCanvas = <HTMLCanvasElement>document.getElementById("main_canvas");
// --- LITERAL TYPES
let x: "hello" = "hello";
// OK
x = "hello";
// ...
x = "howdy";
Type '"howdy"' is not assignable to type '"hello"'.

// --- ANOTHER EXAMPLE
function printText(s: string, alignment: "left" | "right" | "center") {
  // ...
}
printText("Hello, world", "left");
printText("G'day, mate", "centre");
Argument of type '"centre"' is not assignable to parameter of type '"left" | "right" | "center"'.

// --- NUMERIC LITERAL TYPES
function compare(a: string, b: string): -1 | 0 | 1 {
  return a === b ? 0 : a > b ? 1 : -1;
}

// --- COMBINATION OF LITERALTYPES WITH NON LITERAL TYPES
interface Options {
  width: number;
}
function configure(x: Options | "auto") {
  // ...
}
configure({ width: 100 });
configure("auto");
configure("automatic");
Argument of type '"automatic"' is not assignable to parameter of type 'Options | "auto"'.

more info in docs

everyday types from the handBook

This entry was posted in Без рубрики. Bookmark the permalink.