porsager/postgres

Typing issue with nested class variables

Open

#664 opened on Aug 24, 2023

View on GitHub
 (0 comments) (0 reactions) (0 assignees)JavaScript (6,221 stars) (230 forks)batch import
Typescripthelp wanted

Description

If I have:

  type InnerTest = {
      aProp: number
    }
    
    type OuterTest = {
      inner: InnerTest
    }

    const o: OuterTest = {
      inner: {
        aProp: 5
      }
    };

    await sql `INSERT INTO some_table ${this.db(o)}`

Everything works fine.

If I change OuterTest to a class instead of a type, it still works.

But, If I change InnerTest to a class instead of a type, I get:

No overload matches this call.
  Overload 1 of 2, '(first: OuterTest & Partial<Record<"inner", ParameterOrJSON<never>> & Record<string, any>>): Helper<OuterTest, []>', gave the following error.
    Argument of type 'OuterTest' is not assignable to parameter of type 'OuterTest & Partial<Record<"inner", ParameterOrJSON<never>> & Record<string, any>>'.
      Type 'OuterTest' is not assignable to type 'Partial<Record<"inner", ParameterOrJSON<never>> & Record<string, any>>'.
        Types of property 'inner' are incompatible.
          Type 'InnerTest' is not assignable to type 'ParameterOrJSON<never>'.
            Type 'InnerTest' is not assignable to type '{ readonly [prop: string]: JSONValue | ((...args: any) => any); readonly [prop: number]: JSONValue | ((...args: any) => any); }'.
              Index signature for type 'string' is missing in type 'InnerTest'.
  Overload 2 of 2, '(template: TemplateStringsArray, ...parameters: readonly ParameterOrFragment<never>[]): PendingQuery<Row[]>', gave the following error.
    Argument of type 'OuterTest' is not assignable to parameter of type 'TemplateStringsArray'.
      Type 'OuterTest' is missing the following properties from type 'TemplateStringsArray': raw, length, concat, join, and 21 more.ts(2769)
const o: OuterTest
No quick fixes available

The only workaround seems to be to coerce sql to type any instead of postgres.Sql<{}>, but I'm not sure the consequences of that. It seems to basically turn off any type checking altogether.

Contributor guide