mostafa / xk6-kafka

k6 extension to load test Apache Kafka with support for various serialization formats, SASL, TLS, compression, Schema Registry client and beyond
Apache License 2.0
148 stars 63 forks source link

Error importing SCHEMA_TYPES enum when using index.d.ts. #292

Open bloodgang94 opened 4 months ago

bloodgang94 commented 4 months ago

Hello! My project is on typescript. I moved index.d.ts to my project.

When importing SCHEMA_TYPES I get the error TypeError: Cannot read property 'SCHEMA_TYPE_AVRO' of undefined

Example

import { SCHEMA_TYPES, SchemaRegistry } from 'k6/x/kafka'
....
console.log(SCHEMA_TYPES.SCHEMA_TYPE_AVRO)
declare module 'k6/x/kafka' {
  /**
   * @packageDocumentation
   * xk6-kafka is a k6 extension to load test Apache Kafka
   */

  /**
   * @module k6/x/kafka
   * @description
   * The xk6-kafka project is a k6 extension that enables k6 users to load test Apache Kafka using a producer and possibly a consumer for debugging.
   * This documentation refers to the development version of the xk6-kafka project, which means the latest changes on `main` branch and might not be released yet, as explained in [the release process](https://github.com/mostafa/xk6-kafka#the-release-process).
   * @see {@link https://github.com/mostafa/xk6-kafka}
   */

  /** Compression codecs for compressing messages when producing to a topic or reading from it. */
  export enum COMPRESSION_CODECS {
.....

tsconfig

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "load/*": ["./*"]
    },
    "target": "es2017",                        
    "module": "commonjs",                     
    "lib": [                                
      "esnext",
      "dom"
    ],

    "declaration": true,                  
    "declarationDir": "./dist/types",
    "sourceMap": true,                      
    "outDir": "./dist",                     
    "strict": true,                          
    "moduleResolution": "node",               
    "typeRoots": [                           
      "node_modules/@types",
      "./types"
    ],
    "esModuleInterop": true,                  
    "experimentalDecorators": true,       
    "emitDecoratorMetadata": true           
  },

  "exclude": [
    "node_modules"
  ],
  "include": [
    "src",
  ],

}

I found a similar problem

If you change it to constant then there is no error. Do I understand correctly that index.d.ts is not relevant? image

mostafa commented 4 months ago

Hey @bloodgang94,

There is no such symbol named SCHEMA_TYPES and these are the schema types you can import into your code. https://github.com/mostafa/xk6-kafka/blob/1d8e9efcd937e8ab7a476e7546e5c0181e1d3014/module.go#L210-L215

As for the index.d.ts file, I tried to keep it as close to the exported parameters as I can and it is still relevant, AFAIK. I used enums to be able to reference them in other types, but they are actually constants, so you'd better import the actual constant.

bloodgang94 commented 4 months ago

@mostafa , how did you think about fixing index.d.ts? I can help you with this by making a merge request. It would also be nice to publish it there https://github.com/DefinitelyTyped/DefinitelyTyped

mostafa commented 4 months ago

@bloodgang94 Go for it.