Category Archives: Uncategorized

Fields with Inheritance in typescript

If we were to construct our Car, we need to make sure it call the parent class’s constructor. We do this by using super, and passing in whatever required parameters is required. In our case, it is color: string.

We then declare new property wheels for Car.

Lastly, make sure we put in the required constructor params for Car when we instantiate it.

Arrays in Typescript

Typed Arrays – Arrays where each element is some consistent type of value.

Multiple Types in Arrays

When to use Arrays?

When we want to represent a collection of records which some arbitrary sort order.
of same type.

https://stackoverflow.com/questions/49047886/whats-difference-between-array-and-tuple/49047969

Tuple vs Arrays

Tuple – A tuple is a grouping of unnamed, ordered values. Each value in a tuple does not need to be the same type.

Array An array is mutable collection. They are very efficient to create, but must always be a single type.

We have a literal object like this:

type alias

But Why Tuples!?

Here’s why we don’t really use it too much.

It does not describe what those numbers mean. It is not descriptive.

Let’s use object instead. It is much more descriptive because we are forced to put in a key.

Typescript add Annotation onto Functions

Type annotations for functions – Code we add to tell Typescript what type of arguments a function will receive and what type of values it will return.

Type inference for functions – TS tries to figure out what type of value a function will return.

Alternate forms of syntax

Annotate a by providing type number
Annotate b by providing type number
Annotate the return value by providing type number

if no return value, we can specify void

TypeScript introduced a new type never, which indicates the values that will never occur.

Difference between never and void

ref – https://www.tutorialsteacher.com/typescript/typescript-never

The void type can have undefined or null as a value where as never cannot have any value.

A function that has return type void, and no return value, actually defaults to return undefined.

If you you use never type, then sayHi will give a compile time error, as void is not assignable to never.

Destructuring with Annotation

In order to do parameter destructuring, we do this:

Annotation with Objects

Typescript Inference

Typescript Inference

variable declaration: const color
variable initialization: ‘red’

If declaration and initialization are on the same line, Typescript will figure out the type of ‘color’ for us.

But when are we going to add in these type Annotations?

If typescript can figure it out, why would we want to do it yourselves?

Try to use Type Inference.

But there are THREE scenarios where we rely on Type Annotations to help Typescript out.

Scenario 1 – ‘Any’ type

Whenever a function return the any type.

JSON.parse() –> ‘false’ –> boolean
JSON.parse() –> ‘4’ –> number
JSON.parse() –> ‘{“value”, 5}’ –> {value:number}

Can’t predict what we get in return.

So as a shortcut, typescript uses ‘Any’ type.
We have no idea what type gets returned.

type Any – means TS has no idea what this is – can’t check for correct property references

Avoid variables with ‘any’ at all costs.

The reason why is because say we have declare speed to be string:

if we then go

TS will let us know that property abcdef does not exist. Because TS knows that speed is a string.
But with Any, TS doesn’t know, and won’t be able to do this check for you.

Scenerio 2 – How to use Use Type Annotation to Fix the ‘Any’ Type

We can add type annotation for object.

Say we’re expecting an object where there’s a x and y property of type number.

Now if we mouse over coordinates, or try to access a weird random property, we’ll get proper feedback or error.

Delayed Initialization

Another way to add type annotation manually…

Scenario 3 – variable shows Type cannot be Inferred Correctly

what we could do:

webpack and typescript

Webpack is a tool that allows us to bundle our code. Transform our typescript code into javascript, then emit 1 javascript bundle file.

Webpack-dev-server is a development server.

Set up a webpack project

go to tsconfig file.

“target”: “es6”
“module”:”2016″

touch webpack.config.js

webpack will automatically look for this file.

process.nextTick() vs setImmediate()

ref – https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

process.nextTick() vs setImmediate()
We have two calls that are similar as far as users are concerned, but their names are confusing.

process.nextTick() fires immediately on the same phase

setImmediate() fires on the following iteration or ‘tick’ of the event loop

In essence, the names should be swapped. process.nextTick() fires more immediately than setImmediate(), but this is an artifact of the past which is unlikely to change. Making this switch would break a large percentage of the packages on npm. Every day more new modules are being added, which means every day we wait, more potential breakages occur. While they are confusing, the names themselves won’t change.

We recommend developers use setImmediate() in all cases because it’s easier to reason about.

Unpacking fields from objects passed as a function parameter

ref – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

mongo db commands

ref – https://www.tutorialspoint.com/mongodb/mongodb_create_database.htm

MongoDB’s

use some DB name is used to create database. The command will create a new database if it doesn’t exist, otherwise it will return the existing database.

However, you must have at least 1 document in your DB before it shows up in the list. So we create one collection and insert 1 document in order for the DB to exist.

WriteResult({ “nInserted” : 1 })

show dbs

and you’ll see database portfolio listed.

Kafka Node

https://www.npmjs.com/package/kafka-node
kafka-node-hello-world

We first require kafkajs module and extract the class. We then create a topic and group ID.
It is simply to identify the group that will be consuming, and the name of a topic that we’ll be producing.

Then we instantiate it with a unique client id. For brokers let’s use localhost:9092 because that is what we’re using in our Kafka settings file.

get a producer

get a consumer

Consumers Subscribe

Consumer Run

Consumer Unsubscribe

Producer Start

Producer Send Data

Producer Stop