mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-29 00:06:17 -05:00
As per the linked issue, although TypeScript already has checking in place that tries to prevent users from extending classes before their declaration, it's still possible to accidentally work around the checker. This adds a block to `__extends` that throws a `TypeError` if the base class `b` isn't a function.
My _hope_ is that this will not have a negative performance impact on community performance-critical applications, as they would likely already prefer newer browser/Node versions and output ES2015+ code. If there is something you can think of that I should do to verify that hope, I'd love to know!
For reference, runtime errors in Node 12.0.0 (Chrome exhibits the same messages):
```js
class X extends null { }
// undefined
class Y extends undefined { }
// TypeError: Class extends value undefined is not a constructor or null
class Z extends 0 { }
// TypeError: Class extends value 0 is not a constructor or null
```
`Class extends value {0} is not a constructor or null` matches the Node.js behavior:
* [Message template](2bdeb88c27/deps/v8/src/common/message-template.h) for `ExtendsValueNotConstructor`
* [Error thrown with that message](6ca81ad72a/deps/v8/src/runtime/runtime-classes.cc (L617)) when `!super_class->IsConstructor()`
Runtime errors in Firefox 72.0.1:
```js
class X extends null { }
// undefined
class Y extends undefined { }
// TypeError: class heritage undefined is not an object or null
class Z extends 0 { }
// TypeError: class heritage 0 is not an object or null
```