Skip to content

Using $narrowType() is not portable with Typescript composite #1609

@Mikescops

Description

@Mikescops

Hello,

I'm on a Typescript project with composite = true, I'm trying to narrow a type of query because i'm using something like :

        .selectFrom("Envelope")
        .leftJoin("Document", "Document.envelopeId", "Envelope.id")
        .select((eb) => [
            eb.fn
                .coalesce(eb.fn.jsonAgg("Document").filterWhere("Document.id", "is not", null), eb.val([]))
                .as("documents")
        ])

as shared in this thread: #649 (comment)

The issue i get when using :

        .$narrowType<{
            documents: { id: number; uuid: string; title: string; }[];
        }>()

is:

The inferred type of 'getEnvelopesByUserIdInTransaction' cannot be named without a reference to 'libs/api-base/node_modules/kysely/dist/esm/util/type-error'. This is likely not portable. A type annotation is necessary.ts(2742)

This sounds like a similar issue than #1464 where types are not exported for this function.

If I do export in index.ts

export { KyselyTypeError } from './util/type-error.js';

the error disappear and i get KyselyTypeError<"$narrowType() call failed: passed type does not exist in 'documents's type union"> on my final type, which is still an error but now the ball is on my end.

I'm getting this because I wrongly assumed that narrowType would allow me to correctly type the json created by the query (that puts nullable all keys in the inference) but narrowType is limited to the column key.

Do you want me to proceed with a PR for the initial issue here?

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtypescriptRelated to Typescript

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions