I recently covered an easy way to silence TypeScript when dealing with 3rd party libries with missing or incorrect types.

An additional question that post brought up was:

How do I get fix TypeScript "namespace has no exported member" errors?

The problem

Let's say you're using the popular JavaScript game framework Phaser, which has multiple complex deeply nested objects. The types provided are virtually always out of date with the library itself since Phaser is complex, quickly moving and mostly used by people writing JS rather than TS.

With an existing TypeScript project, if you've been updating your types as you go, there's no issue. But when you migrate a project from JS to TS, you'll have a lot of errors to work through. Even a new project on the newest version of the framework can be an editor full of red squiggly lines and type errors.

Worse still places in your code must refer to members of Phaser. Even if you write declare const Phaser: any; at the top of your app to silence all TS errors from importing or using Phaser, you've still got two problems. First, it's overkill and you probably do want to get your app typed, including the Phaser portions at some point. Second, you're probably going to make your own class which has a member of type Phaser.Game. Since Phaser.Game is also very complex and generally ships with incomplete or out of date types, you'll end up with with the "namespace has no exported member error".

The solution

Work from the outside of your 3rd party library in. Disable warnings on member types by exporting their types in the 3rd party library's namespace.

namespace Phaser {
  export type Game = any;
}

export class MyGame {
    private actors: Array<MyActorType>; 
    private actor: <MyActorType>;
    protected game: Phaser.Game;  // TS errors are silenced here now!
    protected manageAssets(): void { };
    // ... the rest of the MyGame class
}

Now, you'll no longer get the error about Phaser.Game. In other modules, where you use members of Game, you can dig deeper with the same process if there are further nested type issues. Eventually you'll get down to simple objects that are trivial to correctly type.

Fixing these issues as you go, you'll get to a point where all the parts in of the 3rd party library that are used in your app will be correctly typed and the others will be silenced. This is a reasonable trade-off with a large 3rd party library in flux, such as Phaser or Materialize.

What's the difference between setTimeout(callback, 0) and process.nextTick(callback)? How about Node's setImmediate(callback)?

On the surface, it appears that all three functions do the same thing—they execute the callback after the current event loop, but before anything else. The natural question to ask is, why are there three different functions? Let's run an experiment:

(more…)

JavaScript's keyword this is a source of pain for a lot of people. A common question goes something like this:

"I make call a Firebase function, and inside that callback, I need to access the this to update my React component."

Another common case is setTimeout:

"The problem is setTimeout takes a callback, but I need to access this and setTimeout changes it!"

(more…)

TypeScript is a tradeoff.

"I use typescript when I feel like having hours of migraines using 3rd party libraries and making sure I don't have a single space or comma out of place! Works like a charm! Get a headache 10/10 times."

-somebody on Reddit
(more…)

Groupon was my first true programming job. I’d worked briefly at a .com way back in the day, and I’d done a variety of tasks at a start-up in Beijing ranging from running human resources to assisting with business development. But Groupon was the first time my entire job was about producing code. Not coincidentally, it was my first job out of Hack Reactor.

(more…)

Visiting the house I grew up in this past holiday season, I had the strangest experience. I found my old notebooks from when I was about 14...and couldn't understand the contents!

I had been a math prodigy, entering college when I was 13. But, after a couple of years as a good college student left for social reasons to become a pretty mediocre high school student. Over time, I ended up focusing on other things and the math skills deteriorated.

(more…)

2013 was a singular experience.

Entering into the year, I was upbeat but still struggling in a lot of ways. I had faced rejection on nearly every front—countless job interviews, a coding school, and even a love interest. I was living in a tiny room in Chinatown worrying about running out of what was left of my savings while I looked for work.

A that made 2013 all the more amazing! I developed new skills, got better schooling than I had hoped, started a great job, made great new friends and reconnected so many people from my past. It wasn’t just me, either. I also had the chance to see friends succeed on a bigger scale than ever before.

I have huge plans for 2014 and I have nothing but excitement about throwing myself in, heart and soul. It’s a day late to say this but Happy New Year, everyone! Thank you so very much for being my friends, through the ups and downs.

I've spent a lot of time on Massive Online Open Courses (AKA MOOCs). I've learned some great things from them, but I've also encountered a lot of time-wasting inefficiencies. For the most part, I've been taking programming and CS-related MOOCs. There are quite a few I looked at and then bailed on before doing any work, but also quite a few I put work into. Below is a list of the classes I worked on and then a summary of each.

Courses Studied

  1. Software Engineering for SaaS (UC, Berkeley)
  2. Introduction to Systematic Program Design (University of British Columbia)
  3. Discrete Optimization (University of Melbourne)
  4. Coding the Matrix: Linear Algebra through Computer Science Applications (Brown)
  5. Algorithms, Part I (Princeton)
  6. Linear and Integer Programming (CU, Boulder)
  7. Functional Programming Principles in Scala (École Polytechnique Fédérale de Lausanne)
  8. Automata (Stanford)
  9. Principles of Reactive Programming (École Polytechnique Fédérale de Lausanne)
  10. Mathematical Biostatistics Boot Camp 1 (John Hopkins) - In progress
  11. Creative, Serious and Playful Science of Android Apps (UI, Urbana-Champaign) - In progress

(more…)