Closed teixeirazeus closed 1 year ago
Same error, just decided to define my classes in a poc_dataclass.ts
file, compiled it into a poc_dataclass.js
file, imported my dataclass and was met with the same error.
I'm mainly a python developer, but decided to focus on nodejs / deno for my side projects... This is not yet production ready in my opinion, but it is a great idea and I'm hoping this project will explode. Dataclasses are great :)
@teixeirazeus,
Looking at the tsconfig.json, you're trying to compile your code to ES5, which doesn't have classes yet. The Data
parent class is being shipped as ES6 (ES2015) class, which doesn't allow calling it as a function. When TypeScript compiles a class to an ES5 constructor function, and the class uses inheritance, TS needs to call parent constructor, which is being done via ParentClass.call(this)
. This is where ES6 class throws an error (since Data is still an ES6 class, while your usage code transpiled to ES5's constructor functions).
Looking at module: esnext
, maybe you don't need to compile your code to ES5, but stick to at least ES6 (ES2015)? It may not be an option if you need to support old browsers like Internet Explorer 11 though. If switching to at least ES2015 is not an option, another way of fixing this behavior is making sure your setup also does transform dependencies to ES5. @nlhnt, if that's just a side project, I bet you'd have better time not compiling code to very old spec.
There were historical reason to abandon shipping ES5 version of dataclass. Dealing with all sorts different setups and spec incompatibilities is nightmare. I know it may not be the answer you're looking for, I'm sorry.
Thanks for replying :)
Hello,
I tried to run the example and got this error:
The code:
Typescript dont allow me to use
new
with the.create
.This is my
tsconfig.json
: