Comments (11)
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.
from code-block-writer.
Confirmed, the updated types work without my wrapper code.
from code-block-writer.
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.
from code-block-writer.
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.
from code-block-writer.
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:
- https://www.typescriptlang.org/docs/handbook/2/functions.html#construct-signatures
- https://stackoverflow.com/questions/13407036/how-does-interfaces-with-construct-signatures-work
from code-block-writer.
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)
from code-block-writer.
This should work in 11.0.2. Sorry about the issues
from code-block-writer.
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.
from code-block-writer.
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, CodeBlockWriter.mts
, which exports the constructor:
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 CodeBlockWriter
, I have to import both the original CodeBlockWriter
to use as a type, and the de-wrapped constructor from the custom export.
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"
}
from code-block-writer.
Actually, I initially thought the code was doing export =
and the types were doing export default
, but the code is doing the exports.default
thing in cjs. I think it should be able to be consumed by doing CodeBlockWriter.default
in the mjs, but I'm not sure why TypeScript doesn't understand it that way because the package is distributed as cjs. I'm just going to convert back to a dual esm/cjs package, but I think I have it right this time (maybe lol). I'll try to do it tomorrow though because it's late here.
from code-block-writer.
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.
from code-block-writer.
Related Issues (20)
- Ability to write with a hanging indent HOT 1
- Error when running `yarn test` HOT 1
- Performance: Move regex to be static
- Add ability to specify max line length HOT 1
- Add closeComment()
- Add `indent(times)`
- Improve Performance HOT 1
- isLastBlankLine() and getLength() should not call toString() HOT 1
- Add method for iterating over the previously written characters
- Should not push empty strings onto internal array
- Add insert method HOT 2
- Rename `withHangingIndentation` to `hangingIndent` HOT 1
- Stays in string even after newline?
- Release for Deno HOT 1
- Declaration and capturing of variables HOT 1
- getBaseTypes() and getBaseClass() not working HOT 2
- Property generic types
- TypeError: CodeBlockWriter is not a constructor HOT 2
- Suggestion: writer.pairedWrite(startToken, endToken, newLine, indent, block: () => void)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from code-block-writer.