Understanding TypeScript Fundamentals

TypeScript extends JavaScript by adding static typing capabilities, cmopiling down to plain JavaScript that runs across all platforms and environmenst.

Acquisition Methods for TypeScript Tools

  1. Install via npm
npm install -g typescript
  1. Install the TypeScript plugin for Visual Studio

In TypeScript, variable types are declared using the colon syntax (:), with optional spacing around it:

const variableName: string = 'la la la';

The void type denotes functions that do not return a value:

function alertName(): void {
  console.log('la la la la');
}

A variable of type void can only be assigned undefined or null:

let unusable: void = undefined;

The any type allows assignment of any value, and accessing properties on any values is permitted. If a variable is declared without an explicit type, it defaults to any:

let anyName: any = 'xtddx';
anyName = 0;
console.log(anyName.aa);

let noneName;
noneName = 'aksbdkas';
noneName = 0;

Type inference occurs when no explicit type is given; TypeScript deduces the type based on usage. Variables initialized without a value are inferred as any and undergo no type checking.

Union types represent values that may belong to one of several specified types, separated by the pipe (|) operator:

let jointName: number | string;
// Property 'length' does not exist on type 'string | number'.
// jointName.length; // Error when calling length method
jointName.toString();
jointName = 'skjd';
console.log(jointName.length); // Works
jointName = 0;
// Property 'length' does not exist on type 'number'
// console.log(jointName.length); // Error

Index signatures allow defining how objects behave when indexed. TypeScript supports both string and numeric index signatures:

interface Person {
  name: string;
  age?: number;
  [propName: number]: any;
}

let tom: Person = {
  name: 'name',
  3: 'aa'
};

interface Person2 {
  name: string;
  age?: number;
  [propName: string]: any;
}

let tom2: Person2 = {
  name: 'name',
  aaa: 'aa'
};

Tags: TypeScript javascript typing programming

Posted on Sat, 03 Oct 2026 16:56:04 +0000 by tonypr100