ts-safeql / safeql

Validate and auto-generate TypeScript types from raw SQL queries in PostgreSQL.

Home Page:https://safeql.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Non-fixable incorrect type annotation and max call stack error when dealing with recursive types

lunedis opened this issue · comments

Hey, sorry, it's me again :)

Describe the bug
safeql exhibits weird behaviors when dealing with recursive types.

config:

overrides: {
  columns: {
    'test.value': 'Entry[]',
  },
},
interface Text {
  type: 'entry';
  name: string;
}

interface Group {
  type: 'group';
  entries: Entry[];
}

type Entry = Text | Group;

sql<{ id: number; value: Entry[] }[]>`
    SELECT 
      id, COALESCE(value, '[]'::jsonb) AS value
    FROM test;`;

This throws an error, that is not auto-fixable:

 21:5  error  Query has incorrect type annotation.
        Expected: { id: number; value: ({ type: 'entry'; name: string } | { type: 'group'; entries: Entry[] })[] }[]
          Actual: { id: number; value: Entry[] }[]  @ts-safeql/check-sql

Furthermore, when changing the overwritten type to 'test.value': 'Entries' (in order to "trick" safeql), running eslint throws an maximum call stack size exceeded error:

ops! Something went wrong! :(

ESLint: 8.57.0

RangeError: Maximum call stack size exceeded
Occurred while linting /home/kalu/bw/safeql-recursive-type-bug/src/main.ts:21
Rule: "@ts-safeql/check-sql"
    at getSourceTextOfNodeFromSourceFile (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:13387:45)
    at getTextOfNode (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:13407:12)
    at declarationNameToString (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:13680:62)
    at getNameOfSymbolAsWritten (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:54415:18)
    at createAccessFromSymbolChain (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:52297:27)
    at symbolToTypeNode (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:52274:28)
    at typeToTypeNodeWorker (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:50900:18)
    at typeToTypeNodeHelper (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:50749:26)
    at typeReferenceToTypeNode (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:51326:33)
    at typeToTypeNodeWorker (/home/kalu/bw/safeql-recursive-type-bug/node_modules/typescript/lib/typescript.js:50905:85)

To Reproduce
See reproduction repo: https://github.com/lunedis/safeql-recursive-type-bug

Expected behavior
I guess it should not throw the error but just "accept" the type alias, and also not run into an endless loop.

Desktop (please complete the following information):

  • OS: Ubuntu 22.04
  • PostgreSQL version: 16.2
  • safe-ql version: 3.2.0

fixed in 3.2.1 by #219

I haven't really thought about recursive types since ideally they should never happen if you're not overriding column types. Hopefully it won't happen again