Coder Social home page Coder Social logo

Comments (11)

dsherret avatar dsherret commented on April 28, 2024 1

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.

ajvincent avatar ajvincent commented on April 28, 2024 1

Confirmed, the updated types work without my wrapper code.

from code-block-writer.

dsherret avatar dsherret commented on April 28, 2024

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.

ajvincent avatar ajvincent commented on April 28, 2024

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.

ajvincent avatar ajvincent commented on April 28, 2024

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:

from code-block-writer.

dsherret avatar dsherret commented on April 28, 2024

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.

dsherret avatar dsherret commented on April 28, 2024

This should work in 11.0.2. Sorry about the issues

from code-block-writer.

ajvincent avatar ajvincent commented on April 28, 2024

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.

ajvincent avatar ajvincent commented on April 28, 2024

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.

dsherret avatar dsherret commented on April 28, 2024

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.

dsherret avatar dsherret commented on April 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.