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
TypeScript error TS2351: This expression is not constructable. #42
Comments
That's strange... I'm not sure why TypeScript is erroring there as the declaration file has a default export for the class. It executes fine in a regular mjs file. |
For all I know, this could be a compiler bug in tsc. I've bumped into one of those already. Would it be helpful for me to provide my tsconfig? What else might I offer to help? (Can you reproduce it? If not, how can I help?) I did search for this compiler error, but it is a very obscure one. |
I wonder if this has to do with my tsconfig, which defaults to: {
"compilerOptions": {
"lib": ["es2021"],
"module": "es2022",
"target": "es2022",
"moduleResolution": "node16",
"sourceMap": true,
"declaration": true,
},
} Also, I found: |
I’m able to reproduce the issue as well. I think I’m just going to revert the esm module because it seems typescript is still buggy with esm. Just this file needs to be updated to only do a cjs build https://github.com/dsherret/code-block-writer/blob/main/scripts/build_npm.ts and then the repo retagged. I will try to remember to do it tomorrow (just falling asleep now) |
This should work in 11.0.2. Sorry about the issues |
I have some more information, which I found buried deep in TypeScript bugs. microsoft/TypeScript#21621 (comment) Basically, import { default as CodeBlockWriter } from "code-block-writer"; should have worked in the old code. |
I'd like to reopen this issue. I think it may have regressed. import CodeBlockWriter from "code-block-writer";
void(new CodeBlockWriter); // TypeError: CodeBlockWriter is not a constructor I found a work-around, based on earlier comments in this issue. First, I created a module, import CBW_, {
type Options as CodeBlockWriterOptions
} from "code-block-writer";
const CodeBlockWriter = (CBW_ as unknown as {
default: new (opts?: Partial<CodeBlockWriterOptions>) => CBW_
}).default;
export default CodeBlockWriter;
export type {
CodeBlockWriterOptions
}; Second, in code wishing to create a import CBW_ from "code-block-writer";
import CodeBlockWriter from "../CodeBlockWriter.mjs";
// ...
export default
abstract class Foo {
readonly #classWriter = new CodeBlockWriter({ indentNumberOfSpaces: 2 });
protected abstract buildMethodBody(
methodName: string,
signature: MethodSignature,
writer: CBW_
) : void;
}
// ... Third, for files just using the API, importing the original CodeBlockWriter as a type is fine: import CodeBlockWriter from "code-block-writer";
import Foo from "./Foo.mjs";
class Bar extends Foo {
protected buildMethodBody(
methodName: string,
signature: MethodSignature,
writer: CodeBlockWriter,
): void
{
void(methodName);
signature.args.forEach(arg => writer.writeLine(`void(${arg.key});`));
writer.writeLine(`throw new Error("not yet implemented");`);
}
} Using TypeScript 5.0.2, with tsconfig: {
"compilerOptions": {
"lib": [
"es2022"
],
"module": "es2022",
"target": "es2022",
"moduleResolution": "node",
"sourceMap": true,
"declaration": true
},
"extends": "@tsconfig/node18/tsconfig.json"
}
|
I just took a look at the types (and I now know way too much about node module resolution than I care to know compared to back in July when I knew basically nothing) and yeah the types are incorrect. |
Actually, I initially thought the code was doing |
Ok, I ended up publishing this tonight anyway. Should be fixed in 12.0.0 as this is now republished as an esm and cjs package, but let me know if it doesn't work. This cjs/esm situation is a huge pain. |
Confirmed, the updated types work without my wrapper code. |
Steps to reproduce:
npm install --save code-block-writer
tsc
for this file.Please advise.
TypeScript version 4.7.4
NodeJS version 16.14.0
The text was updated successfully, but these errors were encountered: