Skip to content
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

TS emits an error for BoolQuery type #100

Closed
dmabuada opened this issue Apr 19, 2021 · 5 comments
Closed

TS emits an error for BoolQuery type #100

dmabuada opened this issue Apr 19, 2021 · 5 comments

Comments

@dmabuada
Copy link

TS expects a should property, despite it being an optional prop in BoolQueryConfig:

interface BoolQueryConfig {
    must?: Query | Query[];
    filter?: Query | Query[];
    should?: Query | Query[];
    must_not?: Query | Query[];
    minimum_should_match?: number | string;
    boost?: number;
}

Doesn't work:

const query: BoolQuery = {
      bool: {
        filter: { match_all: { boost: 1 } },
      },
 }

TS error:

Type '{ filter: { match_all: { boost: number; }; }; }' is not assignable to type 'BoolQueryConfig'.
  Types of property 'should' are incompatible.
    Type 'Assertion' is not assignable to type 'Query | Query[] | undefined'.
      Type 'Assertion' is missing the following properties from type 'Query[]': pop, push, concat, join, and 25 more.

Works:

    const query: BoolQuery = {
      bool: {
        should: undefined,
        filter: { match_all: { boost: 1 } },
      },
    }

TS version: 4.0.3

@jacobwgillespie
Copy link
Owner

Hey thanks for opening this issue, I'm happy to take a look!

I think there may be additional code affecting those types. For instance, the TypeScript error mentions an Assertion type - that type doesn't exist inside elastic-ts, and may be coming from your app code or one of your dependencies.

If I paste your example into the test in elastic-ts it does work.

So the key is probably looking more into what defines Assertion and BoolQuery in your app code. Feel free to share whatever you're able and I can investigate too.

@mgonzalez121690
Copy link

Hi, guys... I was facing the same error and noticed this happens when I ran this command npm i -D @types/chai , so I uninstalled @types/chai in order to fix the problem, and works again.

@jacobwgillespie
Copy link
Owner

Ah, nice find @mgonzalez121690, yeah if you take a look at the very bottom of https://unpkg.com/browse/@types/chai@4.2.17/index.d.ts, it looks like the chai types are adding a should property to literally all objects via the Object interface.

I'm not sure there's anything we can do about that here, chai adding that property to all object types is very broad.

Most likely if you still want to include @types/chai in your project, you would want to adjust the types property in your tsconfig.json to not include @types/chai for your main app code that uses elastic-ts, and have a separate tsconfig.json that you use for your tests. Something like:

tsconfig.json

{
  "compilerOptions": {
    "types": [] // or ["node"] etc, basically a list of global types to evaluate
  }
}

tsconfig.test.json

{
  "compilerOptions": {
    "types": ["chai"] // or ["node", "chai"] etc
  }
}

@jacobwgillespie
Copy link
Owner

I'm going to close this issue, but let me know if the above helps or if you're still experiencing issues!

@nwaughachukwuma
Copy link

Yeah, it's a case with Chai.Assertion polluting the global Object interface. You can neutralize the effect in a declaration file, like so:

// global.d.ts
interface Object {
  should?: any
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants