• Typescript

Filed under:

Syntax

  • ! = non-null assertion operator (this expression cannot be null or undefined)

  • as - typescasting.

    • ex: const input1 = document.getElementById("num1")! as HTMLInputElement

    • The above says, find this element I know is not null, and I know is an HTML Input element.

  • ts adds tuples which are fixed-length arrays.

    • ex: let x: [number, string] = [10, "foo"]

    • typescript can't catch using x.push() as a type error.

  • protected enables accessing properties from inherited classes in child classes (whereas private can't.)

  • abstract classes (and methods) are used to force all future-inheriting classes to re-define how a method should look / work for each individual class.

  • constructor can be made into a private constructor. This prevents the use of instantiating via new

    • You could then create a private static property on a class ... of the class itself.

    • then create a static method of getInstance which will return the instance.