Skip to content

Typing issue with nested class variables #664

Open
@kryptus36

Description

@kryptus36

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @porsager@kryptus36

        Issue actions

          Typing issue with nested class variables · Issue #664 · porsager/postgres