-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issues with generated constructor using this
before calling super()
#1918
Comments
This case is just not implemented. The class transform used by esbuild is modeled after TypeScript's class transform. If you try this in TypeScript, you get the following compile error:
I haven't implemented it because the implementation is complex and very few people actually write code this way, especially because it's just not allowed in TypeScript in the first place. You're actually the first person to ask about this even though esbuild has been around for years. The class transform in esbuild only recognizes export class Kysely extends QueryCreator {
#props;
constructor(args) {
var _super, _props;
if (isKyselyProps(args)) {
_super = { executor: args.executor, parseContext: args.parseContext };
_props = freeze({ ...args });
}
else {
const dialect = args.dialect;
const driver = dialect.createDriver();
const compiler = dialect.createQueryCompiler();
const adapter = dialect.createAdapter();
const log = new Log(args.log ?? []);
const parseContext = new DefaultParseContext(adapter);
const runtimeDriver = new RuntimeDriver(driver, log);
const connectionProvider = new DefaultConnectionProvider(runtimeDriver);
const executor = new DefaultQueryExecutor(compiler, connectionProvider, args.plugins ?? []);
_super = { executor, parseContext };
_props = freeze({
config: args,
executor,
dialect,
driver: runtimeDriver,
parseContext,
});
}
super(_super);
this.#props = _props;
}
} |
Gotcha, thanks for the help. I'll open up a PR with the Kysely library to see if they are open to the slight refactor. Have a good one! |
@evanw Kysely is written in typescript. It's allowed in typescript. |
Ah, I see. You have to set |
Another scenario where I ran into this problem: Inheriting from As a workaround, I removed the Not working, with the same error as in the OP (this does work if compiled with abstract class MyFunctionSubclass extends Function {
public invoke!: this;
//@ts-expect-error
constructor() {
const myFunc = function invokeFunc(this: any, ...args: any) {
// .. do stuff
} as unknown as this;
myFunc.invoke = myFunc;
Object.setPrototypeOf(myFunc, new.target.prototype);
return myFunc;
}
} Working: abstract class MyFunctionSubclass {
public invoke!: this;
constructor() {
const myFunc = function invokeFunc(this: any, ...args: any) {
// .. do stuff
} as unknown as this;
myFunc.invoke = myFunc;
Object.setPrototypeOf(myFunc, new.target.prototype);
return myFunc;
}
}
// @ts-ignore
ApiStateBase.prototype.__proto__ = Function; As a potential solution for esbuild, perhaps don't emit |
Seems to be a similar issue to #242, #885, and #1497 where the generated code (generated with
./node_modules/.bin/esbuild app.ts --bundle --target=es2020 --outfile=app.js
in this case) is transformed such thatthis
is called beforesuper()
.The error produced is:
Example using Kysely library (https://replit.com/@samsouder/LastUnpleasantQbasic) is that this snippet:
is transformed into this invalid snippet:
Would appreciate any help you can lend here.
The text was updated successfully, but these errors were encountered: