TypeScript: Cannot create an instance of an abstract class2 min read

Let A: abstract Class and B: B extends A. Now try writing a function with return type of (typeof A) and returning (typeof B)

Extending an abstract class

//Type Input
abstract class Input {
constructor() {
}
}
//Input Select
class Select extends Input {
constructor() {
super();
}
}
//Input Text
class TextField extends Input {
constructor() {
super();
}
}
//Create a new element from string name
let getInputType = (type: string): typeof Input => {
switch (type) {
case 'select':
return Select;
break;
default:
return TextField;
}
}
let elemType = getInputType('select');
//Create new elem
let elem = new elemType();

I get the following error

let elemType: new () => Input
Cannot create an instance of an abstract class.

Solution

Store the type of Input in a variable InputClass

type InputClass = typeof Input;

Extend the type with an Interface InputLike

interface InputLike extends InputClass { };

Result

//Type Input
abstract class Input {
constructor() {

}
}

type InputClass = typeof Input;
interface InputLike extends InputClass { };

//Input Select
class Select extends Input {
constructor() {
super();
}
}

//Input Text
class TextField extends Input {
constructor() {
super();
}
}

//Create a new element from string name
let getInputType = (type: string): InputLike => {
switch (type) {
case 'select':
return Select;
break;
default:
return TextField;
}
}

let elemType = getInputType('select');

//Create new elem
let elem = new elemType();

EPSON DOT matrix char code to set page length in inches (Javascript)51 sec read

Set of codes to set page length

Find the list of char codes at http://stanislavs.org/helppc/epson_printer_codes.html

So to set the length of a page in number of lines, use

var num_lines = 72;
String.fromCharCode(27, 67) + String.fromCharCode(num_lines)

Going by the char code list to set the length of the page in inches, we should use

var page_inches = 12;
String.fromCharCode(27, 67, 48) + String.fromCharCode(page_inches);

But it just doesn’t work!!!!!

What happens is the printer thinks you want to set the page length to 48 lines and not 12 inches. Also char code 12 means form feed, so you get one of that too..

I solved it by using

var page_inches = 12;
String.fromCharCode(27, 67, 0) + String.fromCharCode(page_inches);

So you need to add String.fromCharCode(0) and not String.fromCharCode(48) and you will get a page length of 12 inches.

 

Creating an SVG filter icon1 min read

I find it easier tot write an HTML page than create a document or powerpoint presentation. Similarly it is easier for me to write SVG icons in XML rather than create one in photoshop. So here is my simple “SVG filter icon”

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100">

</svg>

Start by opening the svg tag and give it a width and height of 100

Next lets add a circle with a radius of 50 and it’s center coinciding with the svg element’s.

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100">
    <circle cx="50" cy="50" r="50" fill="#1b3131" />
</svg>

svg-circle

Now lets add the filter icon using a polygon. We write the co-ordinates of each point of the filter icon, magics happens and we get a filter.

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100">
    <circle cx="50" cy="50" r="50" fill="#1b3131" />
    <polygon points="20,25 80,25 55,60 55,75 45,85 45,60" fill="#a9d41e" />
</svg>

 

We start from the top left corner of the icon (20, 25) and trace its path through (80,25) (55,60) (55,75) (45,85) (45,60) and is finally joined back to (20, 25)

svg-filter

Complete SVG filter icon

Good day/night 🙂