Understanding TypeScript Records

 Understanding TypeScript Records (ricardofilipe.com)


A definition of Record

Typescript 2.1 introduced the Record type, and the official documentation defines it as:

Constructs a type with a set of properties K of type T. This utility can be used to map the properties of a type to another type.

Its definition shows how it works internally, but it can a little scary to newcomers to the language:

type Record<K extends string, T> = {
[P in K]: T
}

But let’s start with a practical example. Consider the following JS object:

const object = {
prop1: 'value',
prop2: 'value2',
prop3: 'value3',
}

If we know the types that both keys and values of that object will receive, typing it with a Record can be extremelly useful. A Record<K, T> is an object type whose property keys are K and whose property values are T.

One post on StackOverflow that initially helped me understand what Record did was this post, in which it’s clear to see that the following type definition:

type PropResponse = Record<'prop1' | 'prop2' | 'prop3', string>

Is pretty much the same as writing this, which you’re probably already familiar with as a normal type definition:

type PropResponse = {
prop1: string
prop2: string
prop3: string
}

Let’s go back to our object we want to type. We know that it has 3 keys, prop1prop2 and prop3, and that each of them has the value of a string. We can use the previous PropResponse to type it, like so:

type PropResponse = Record<'prop1' | 'prop2' | 'prop3', string>
const object: PropResponse = {
prop1: 'value',
prop2: 'value2',
prop3: 'value3',
}

Notice that if we change any of the values to a boolean, TypeScript will not compile:

const object: PropResponse = {
prop1: 'value',
prop2: 'value2',
prop3: true, // Type 'true' is not assignable to type 'string'
}

Of course, very often an object is a mixed bag of types, where you’ll get strings, numbers, booleans and so on. Record still works in these cases, because it accepts a type as one of its values. Let’s look at a more complex example.

The classic Store example

Let’s switch to the classic Store example, with real life data. We’d like to type the in-store availability of products, grouped by ID. Each ID has an object as a value, with availability typed as a string and the amount available for each product.

const store = {
'0d3d8fhd': { availability: 'in_stock', amount: 23 },
'0ea43bed': { availability: 'sold_out', amount: 0 },
'6ea7fa3c': { availability: 'sold_out', amount: 0 },
}

We want to do a few things to type this correctly. We must:

  • Type the key as the product ID, as a string
  • Type the value with a range of availability types
  • Type the amount as a number
// Our product ID will be a string
type ProductID = string
// Defining our available types: anything out of this range will not compile
type AvailabilityTypes = 'sold_out' | 'in_stock' | 'pre_order'

We can also define the Availability as a type itself, containing a value which will be one of the AvailabilityTypes and contain the amount as a number:

interface Availability {
availability: AvailabilityTypes
amount: number
}

💡 Aside: note that we could have also inlined our stock strings instead of creating a new type entirely. The following would have also worked:

interface Availability {
availability: 'sold_out' | 'in_stock' | 'pre_order'
amount: number
}

💡

And we put it all together in a Record type, where the first argument is for our key (ProductID) and the second is for its value (Availability). That leaves us with Record<ProductID, Availability> and we use it like so:

const store: Record<ProductID, Availability> = {
'0d3d8fhd': { availability: 'in_stock', amount: 23 },
'0ea43bed': { availability: 'sold_out', amount: 0 },
'6ea7fa3c': { availability: 'sold_out', amount: 0 },
}

Here’s the full typing for this example:

// types.ts
type ProductID = string
type AvailabilityTypes = 'sold_out' | 'in_stock' | 'pre_order'
interface Availability {
availability: AvailabilityTypes
amount: number
}
// store.ts
const store: Record<ProductID, Availability> = {
'0d3d8fhd': { availability: 'in_stock', amount: 23 },
'0ea43bed': { availability: 'sold_out', amount: 0 },
'6ea7fa3c': { availability: 'sold_out', amount: 0 },
}

(you can play with the playground here)

There are other ways to go about and type this object of course, but Record itself proves to be a useful abstraction that will check keys and value types for us.

Check out the official documentation for more examples and I’ll soon be back for more starter guides on TS! 🎉

Difference between and Apache directives

 Difference between <Location> and <Directory> Apache directives - Server Fault

Directory directive works only for filesystem objects (e.g. /var/www/mypage, C:\www\mypage), while Location directive works only for URLs (the part after your site domain name, e.g. www.mypage.com/mylocation).


The usage is straightforward - you would use Location if you need to fine tune access rights by an URL, and you would use Directory if you need to control access rights to a directory (and its subdirectories) in the filesystem.

How can I configure apache so that it prevents proxy servers to cache a specific URL on my website?

 How can I configure apache so that it prevents proxy servers to cache a specific URL on my website? - Server Fault


This is pretty easy to do with apache, for example to specific that nothing coming from your server should ever be cached, try this

<Location / >
   ExpiresActive On
   ExpiresDefault "now"
</Location>

You probably don't want to do this exactly, so look at the documentation for mod_expires and customise this for your needs.

Also, remember that in this scenario a browser and a proxy are treated the same. By sending expires headers with your responses that disable caching, you will be adding significant load and cost to running your site, not to mention slowing access your visitors.

Create a branch from a Commit

 Git create new branch from master or current branch (mytrashcode.com)


Create a branch from a Commit

To create a branch from a commit, we can use simply pass the commit hash to the checkout command. This will ignore all the changes made above and after that commit.

So your new branch will only have changes until the commit you specify. It will ignore all the changes post that.

Here’s what the command looks like.

git checkout master
git pull
git checkout -b <New_branch_name> <commit_hash_id>

Creating a branch from a release tag

Similar to creating a branch from commit, you can also create a commit from one of the release tags.

Here is how the command looks like.

git checkout master
git pull
git checkout -b <New_branch_name> <tag_version>

Example:

git checkout -b new_branch v1.0.0

Create a new branch from the current branch

 Git create new branch from master or current branch (mytrashcode.com)


Create a new branch from the current branch

To create a GIT branch from the current branch, you can use three commands sequentially and expect git to create a new branch for you.

git checkout master
git pull
git checkout <already_exisiting_branch>
git checkout -b <New_branch_name>

How this works :

  • It will first take you to master and pull the latest changes for all the branches of the repo.
  • Then move to an existing branch
  • Then creates a new branch from the existing branch with all the changes of the original branch.

There are few other ways you can create a branch. One is from a single commit and the other one is from any release tag.


How do I create a new branch based on some existing one?

If you want to base your new branch on a different existing branch, simply add that branch's name as a starting point:

$ git branch <new-branch> <base-branch>

If you're using the Tower Git client, you can simply use drag and drop to create new branches (and to merge, cherry-pick, etc.):

Nightingale

 nightingale-rest-api-client/localhost.md at master · jenius-apps/nightingale-rest-api-client (github.com)


In an effort to increase security, Microsoft has blocked modern applications from connecting to localhost by default.

Fortunately, there is a temporary workaround available. Please run this command in command prompt after installing Nightingale:

checknetisolation loopbackexempt -a -p=S-1-15-2-2472482401-1297737560-3464812208-2778208509-1273584065-1826830168-474783446

If you continue to see connection errors to localhost after running the above command, try disabling SSL validation in Nightingale's settings. This is often needed when connecting to a local IIS Express server.

We apologize for the inconvenience, and we appreciate your understanding as we work towards a better solution.

Javascript: Date compare

 Compare two dates with JavaScript - Stack Overflow


The easiest way to compare dates in javascript is to first convert it to a Date object and then compare these date-objects.

Below you find an object with three functions:


  • dates.compare(a,b)

    Returns a number:

    • -1 if a < b
    • 0 if a = b
    • 1 if a > b
    • NaN if a or b is an illegal date


  • dates.inRange (d,start,end)

    Returns a boolean or NaN:

    • true if d is between the start and end (inclusive)
    • false if d is before start or after end.
    • NaN if one or more of the dates are illegal.
  • dates.convert

    Used by the other functions to convert their input to a date object. The input can be

    • date-object : The input is returned as is.
    • an array: Interpreted as [year,month,day]. NOTE month is 0-11.
    • number : Interpreted as number of milliseconds since 1 Jan 1970 (a timestamp)
    • string : Several different formats is supported, like "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
    • an object: Interpreted as an object with year, month and date attributes. NOTE month is 0-11.

StaticImage

  import React , { useEffect , useRef } from "react" import { StaticImage } from "gatsby-plugin-image" impor...