Typing an Array with a union type in TypeScript?

class Apple { appleFoo: any; } class Orange { orangeFoo: any; } var arr : Array<Apple|Orange> = []; var apple = new Apple(); var orange = new Orange(); arr.push(apple); //ok arr.push(orange); //ok arr.push(1); //error arr.push(“abc”); // error var something = arr[0]; if(something instanceof Apple) { something.appleFoo; //ok something.orangeFoo; //error } else if(something instanceof Orange) { … Read more

What is the difference between *.d.ts vs *.ts in typescript?

TypeScript declaration file (*.d.ts) These files are used for describing the “shape” of a JavaScript file for use in TypeScript. For example, say I have the following JavaScript code in a file somewhere outside the scope of what the TypeScript compiler knows: function displayMessage(message) { alert(message); } With this file alone, my TypeScript code won’t … Read more

Is TypeScript really a superset of JavaScript?

The reason for TypeScript’s existence is to have a compiler and language which can enforce types better than vanilla Javascript does. Any regular Javascript is valid TypeScript, syntactically. That does not mean that the compiler must be entirely happy with it. Vanilla Javascript often contains code which is problematic in terms of type security. That … Read more

Getting error TS2304: Cannot find name ‘Buffer’

Add this line at top: declare const Buffer and it should compile without errors. Declarations is required to use node built in libraries or other global objects, you can manually declare it like above. With new version of Typescript, you can also use official declaration files: npm i -g typescript@next npm i –save-dev @types/node for … Read more

Call an overridden method in base class constructor in typescript

The key is calling the parent’s method using super.methodName(); class A { // A protected method protected doStuff() { alert(“Called from A”); } // Expose the protected method as a public function public callDoStuff() { this.doStuff(); } } class B extends A { // Override the protected method protected doStuff() { // If we want … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)