The Insider Threat Perspective

Insider Threat can be broken out into a few behavior categories. Malicious Behaviors, Negligent or Careless Behaviors, Compromising Behaviors.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Why should you use TypeScript over JavaScript?

Image from TSH(The Software House)

TypeScript is a programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. TypeScript is designed for the development of large applications and transcompiles to JavaScript.

Stricter programming languages will inform the developer when they change one area of code in a way that will break other areas. JavaScript will not, which often leads to unexpected behavior at runtime.

To address these shortcomings, Microsoft developed TypeScript and released it publicly in 2012 to blend the flexibility of JavaScript with the advantages of a stricter language.

This article will give you a brief overview of the benefits that TypeScript offers. Although it has plenty of useful features, we will keep it short and discuss only the basic ones.

Consider the JavaScript code :

Our code has a bug 🙀! We need to inspect our code and fix it. It looks like we’re assigning the wrong type of data to dividend. Change the assigned value of dividend to be the expected value with the correct type.

The snippet above has only a few lines of code, so we were able to fix this within no time but what if we are dealing with huge a codebase and happen to face a problem like this. It would be very tedious to look through the codebase and find the error.

Don’t worry :) this is when TypeScript comes into play.

Type inference assumes the expected type of the variable throughout a program based on the data type of the value initially assigned to it at declaration. Type inference will log a complaint if the variable is later reassigned to a different type.

Adding a type annotation ensures that a variable can only ever be assigned to that type. This can be useful when declaring a variable without an initial value. Type annotations get removed when compiled to JavaScript. The type declaration is provided by appending a variable with a colon (:) and the type (eg. number).

When we declare a function in JavaScript, we often expect it to be invoked with arguments of a certain type. JavaScript does not share our expectations, its type flexibility often allows functions to be invoked with unexpected argument types. Even when this doesn’t result in errors, there can be negative consequences:

Although we can approach error-handling solutions to avoid such undesirable effects, these techniques can be cumbersome:

In the code above, the function first checks the argument type. If it doesn’t match the expected type, an error is thrown; otherwise, it continues with its intended operation.

We can handle the above code very neatly with TypeScript using the TypeScript Function Parameter Type Annotations.

Function parameters may be given type annotations with the same syntax as variable declarations: a colon next to the name. The type annotations ensure that the parameters are of the correct type:

If we’d like to be explicit about what type a function returns, we can add an explicit type annotation after its closing parenthesis. Here, we use the same syntax as other type annotations, a colon followed by the type. TypeScript will produce an error for any return statement in that function that doesn’t return the right type of value.

Consider the JavaScript Code:

The function getUserChoice() gets a user’s vehicle choice entered as a number from 1 to 12. Then, returnVehicle() runs a vehicle-generating function (f1() through f12()) to return a filename '<vehiclename>.jpg'.

Imagine only 11 out of 12 of the functions f1() through f12() correctly return a vehicle.jpg filename and one function has a faulty return value of type number but we don’t know which function it is? It would be a nightmare to have to look through all that source code! Let TypeScript do the work for you.

As all the functions are expected to return a value of type string, annoying the function returnVehicle( )as string type will handle the problem.

TypeScript Error

Yeah 😲 !!! TypeScript found the problematic function for us. Now we can just navigate to the f9() function in the codebase and fix its return value.

The type annotation for a one-dimensional array in TypeScript is similar to a primitive data type, except we add a [] after the type.

Consider the JavaScript code:

This is a small piece of code from a company’s codebase

The employeeArray stores the employee’s names that are working under the company. It will crash the entire system if employeeArray is found to contain any data of the number type!

The functioncheckEmployeeArray() checks that every element of employeeArray is a string.

The stringPush()function that takes one argument: val, the value meant to be pushed into the array. The function should check if the val is a string and .push() it into the employeeArray array only if it is a string.

Sadly 😥 even with all this, people could ruin your precious string array by writing code like employeeArray[3]=3

But don’t worry we got TypeScript there to help us out 💪

The same code in TypeScript can be written as :

Now we no longer need thecheckEmployeeArray() function to check that every element of employeeArray is a string or the stringPush()function to verify if an element is a string before pushing it into the array. TypeScript does most of the work for us ❤

TypeScript offers so many functionalities that it becomes a good alternative to JavaScript.

If you have a huge project that you are working on alone or along with several developers, TypeScript is the best alternative.

I hope you found this article to be helpful. If you’d like to learn more, I encourage you to check out the resources below.

Add a comment

Related posts:

Virtual Reality Video Camera Will be Launched in Barcelona

To view full content, click => https://9to5game.com/2021/01/10/virtual-reality-video-camera-will-be-launched-in-barcelona/?feed_id=22&_unique_id=6043030e892db A collection of samples out on the table…

living the darkness

when depression taking all the bright side of your life, you can only hold on to one little star and hoping that this could save your life, just for today. living in despair when everything around…

juvenil

sigo buscando al que ha dicho. “juvenil” is published by Camila Hidalgo.