porsager / porsager/postgres

Typing issue with nested class variables

Open
#664 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted Typescript
Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

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

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the sql tagged-template call and the postgres.Sql<{}> typing shown in the report. Reproduce the nested-class case, then compare it with the working type-alias and outer-class cases to trace the incompatible ParameterOrJSON inference. Done means the class-based nested value is accepted without coercing sql to any while preserving type checking.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
databases, developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.