Sitemap

Devlog #3

Better Meta

1 min readJul 28, 2018

--

Today I went down the rabbit hole a bit on a weird undefined reference bug that I was getting when loading a component in Storybook. I had an @input() property, the emitted metadata for which was not correctly referencing its type. When emitDecoratorMetadata is set to true in your tsconfig, then for properties like this…

@Input()
get someProp() { return this._someProp; }
set someProp(value: SomeClass) { this._someProp = value; }

TypeScript emits metadata like this…

import { SomeClass } from './some-module';__decorate([
Input(),
__metadata("design:type", SomeClass),
__metadata("design:paramtypes", [SomeClass])
], MyComponent.prototype, "someProp", null);

Or at least, it’s supposed to. In turned out that, with this particular construction, design:type was Object, and the SomeClass class was elided from the import. That was what was causing the runtime reference error, because the design:paramtypes line attempted to reference the variable SomeClass, which didn’t exist in any surrounding scope.

Get Jon Rimmer’s stories in your inbox

Join Medium for free to get updates from this writer.

At first I thought this was a Storybook error, then I figured it was caused by the ts-loader Webpack loader that uses. The imported symbols in a bundle are mangled with a bunch of _WEBPACK_ crap, and I figured that it was Webpack itself that was somehow missing the SomeClass reference. But I eventually figured out that it was the TypeScript compiler itself that was failing to include the import.

The happy ending is that I raised it as a bug, and it got fixed just a few hours later. That’s an impressive turnaround from the the TypeScript team, I have to say.

--

--