#How to stop inferring

30 messages · Page 1 of 1 (latest)

hexed totemBOT
#
ciantic#0

Preview:```ts
function test<
T extends Record<string, string | (() => string)>

(t: T) {
return t
}

// Pre-requisite, when hovering foo or bar, it should show the original thingie like { "first_name": string, get_name: () => string }

// THIS WORKS! YAY
const
...```

vapid swift
#

This is continuation of my earlier problem

#

Now I'm interested to know, is there more workarounds to make TypeScript inference to stop early

dire moat
#

i was wondering before too: do you actually need to write code like this? if so, why? at first glance it seems somewhat pointless

#

or is this more just a curiosity thing

vapid swift
#

at this point it's more of curiosity, but yes I do have an use case, it's quiet complex but it's self-referencing foreign key @dire moat

dire moat
vapid swift
#
const personTable = table("person", {
    id: pkAutoInc(),
    first_name: col(v.string()),
    last_name: col(v.optional(v.string())),
    // Self referencing foreign key, requires untyped `foreignKeyUntyped`
    supervisor_id: foreignKeyUntyped(col(v.number()), "person", "id"),
    created_at: createdAt(),
    updated_at: updatedAt(),
});

// Alternative you can use mutational syntax, which is typed
personTable.columns.supervisor_id = foreignKey(personTable, "id");
#

as I write below, there is an mutational and safe way to do it

#

but it won't retain the types

#

I'm on a quest to combine Valibot and Kysely, works already very well, just that table and it can infer everything!

#

It can infer insert validator, update patch validator, update key schema validtor (tuple of rowversion and primary key), columns, and all is still typed

#

(now I'm working on adding navigations between database tables)

dire moat
#

ah i see, for some reason i thought "self-referencing" might mean a field referencing itself, but it's a field referencing its containing object (which makes more sense given your original question)

vapid swift
#

In reality I probably don't need it, but having some safe way to reference from supervisor_id => id would be nice

#

it's not typical to have self-referencing foreign keys

dire moat
#

IIRC drizzle (which i'm more familiar with) uses a callback for foreign keys, so the analogy here might look something like supervisor_id: foreignKey(() => personTable.id) ( guess it'd have to be marked as nullable too, otherwise you could never create the first person 😆)

#

that also lets you have two tables which mutually reference eachother

vapid swift
#

oh that looks neat!

#

I really like this

type Foo = {
    title?: string | undefined;
    description?: string | undefined;
    due_date?: Date | undefined;
} & {
    id: number;
    rowversion: number;
}

That it can infer that type

#

I don't think many other ORMs can infer rowversion as requirement

vapid swift
#

I converted everything to callbacks, and now it's still complaining self-referencing

#

also same problem if I add more wrappers:

vapid swift
#

Interestingly Drizzle has same problem!

#

Drizzle solved it with, ..., drum roll, ... manual types!

dire moat
#

oh yeah, now that i look at some old code (haven't had to touch postgres in >6 months) i see i had to annotate in a couple places too

#

my bad for suggesting that might avoid the problem