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

JSONObject-type enforces Index signature on all interfaces used in the defineEventHandler() return statement. #112

Closed
ArthurMJ96 opened this issue Apr 30, 2022 · 1 comment

Comments

@ArthurMJ96
Copy link

The JSONObject-type (here) requires us to add an Index signature to all interfaces used in the defineEventHandler() return statement.
Forcing us to add [key: string]: any; to our Interfaces allows for errors later on.

Example:

interface Product {
  id: string
  name: string
}

export default defineEventHandler(async (_event) => { // Error given at this point (See below)
  const products: Product[] = [] // Pretend we got some products from the database
  return {
    products,
  }
})
# Error
Argument of type '(_event: CompatibilityEvent) => Promise<{ products: Product[]; }>' is not assignable to parameter of type 'EventHandler<H3Response>'.
  Type 'Promise<{ products: Product[]; }>' is not assignable to type 'H3Response'.
    Type 'Promise<{ products: Product[]; }>' is not assignable to type 'Promise<_H3Response>'.
      Type '{ products: Product[]; }' is not assignable to type '_H3Response'.
        Type '{ products: Product[]; }' is not assignable to type 'JSONObject'.
          Property 'products' is incompatible with index signature.
            Type 'Product[]' is not assignable to type 'JSONValue'.
              Type 'Product[]' is not assignable to type 'JSONArray'.
                Type 'Product' is not assignable to type 'JSONValue'.
                  Type 'Product' is not assignable to type 'JSONObject'.
                    Index signature for type 'string' is missing in type 'Product'.ts(2345)

This would remove the error on our side. But this doesn't really represent our type and allows for more errors later on.

interface Product {
  [key: string]: any;
  id: string
  name: string
}
@pi0
Copy link
Member

pi0 commented May 4, 2022

Thanks for the report. I think we can keep H3Reponse to simple any (same as JSON.stringify) until finding a reliable way to type-check JSON objects.

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

2 participants