How To Use TypeScript to Avoid Bugs
Type Checks

Also, you can put any argument into your JavaScript functions, which brings the same difficulties as the dynamic types as there is no enforcement of what is passed in.

This creates problems as arguments that you’re supposed to pass in but didn’t will be undefined and then you get undefined errors. There is also nothing stopping you from passing in too many arguments.

With both checks, TypeScript makes code easier to understand and follow. You don’t have to worry about breaking things when you change code as the compiler will tell you that you got those basic errors.

In addition, dependency injection is part of TypeScript, which means you don’t have to resolve dependencies yourself. It also makes mocking dependencies easy for testing.

TypeScript provides features in the upcoming versions of JavaScript (which is not finalized yet) that might be handy for some developers.

TypeScript adds types to your objects by defining type files for your objects. It works by setting up a module and then including a module.d.ts file with it.

An example of this is in the TypeScript docs.

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]

// Project: [~THE PROJECT NAME~]

// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]> /*~ This is the module template file. You should rename it to index.d.ts

*~ and place it in a folder with the same name as the module.

*~ For example, if you were writing a file for "super-greeter", this

*~ file should be 'super-greeter/index.d.ts'

*/ /*~ If this module is a UMD module that exposes a global variable 'myLib' when

*~ loaded outside a module loader environment, declare that global here.

*~ Otherwise, delete this declaration.

*/

export as namespace myLib; /*~ If this module has methods, declare them as functions like so.

*/

export function myMethod(a: string): string;

export function myOtherMethod(a: number): number; /*~ You can declare types that are available via importing the module */

export interface someType {

name: string;

length: number;

extras?: string[];

} /*~ You can declare properties of the module using const, let, or var */

export const myField: number; /*~ If there are types, properties, or methods inside dotted names

*~ of the module, declare them inside a 'namespace'.

*/

export namespace subProp {

/*~ For example, given this definition, someone could write:

*~ import { subProp } from 'yourModule';

*~ subProp.foo();

*~ or

*~ import * as yourMod from 'yourModule';

*~ yourMod.subProp.foo();

*/

export function foo(): void;

}
['John Au-Yeung']
['Software', 'Programming', 'Typescript', 'JavaScript', 'Technology']