ditojs / dito

Dito.js is a declarative and modern web framework with a focus on API driven development, based on Objection.js, Koa.js and Vue.js – Developed at Lineto by Jürg Lehni and made available by Lineto in 2018 under the MIT license
https://lineto.com
MIT License
82 stars 8 forks source link

Fix errors in server typings #99

Closed lehni closed 1 year ago

lehni commented 1 year ago

We have fixed most server typings, but one problem remains.

Running yarn types in the server package prints:

➜  server git:(master) ✗ yarn types
types/index.d.ts:1838:33 - error TS2536: Type '$Key' cannot be used to index type 'T'.

1838   [$Key in SelectModelKeys<T>]: T[$Key] extends Model
                                     ~~~~~~~

types/index.d.ts:1839:29 - error TS2536: Type '$Key' cannot be used to index type 'T'.

1839     ? SelectModelProperties<T[$Key]>
                                 ~~~~~~~

types/index.d.ts:1840:7 - error TS2536: Type '$Key' cannot be used to index type 'T'.

1840     : T[$Key]
           ~~~~~~~

This is all due to the following types:

export type SelectModelProperties<T> = {
  [$Key in SelectModelKeys<T>]: T[$Key] extends Model
    ? SelectModelProperties<T[$Key]>
    : T[$Key]
}

export type SelectModelKeys<T> = AnyGate<
  T,
  Exclude<
    keyof ConditionalExcept<T, Function>,
    |  `$${string}`
    | 'QueryBuilderType'
    | 'foreignKeyId'
  >,
  string
>

What I find interesting is that this all looks quite similar to what Objection.js does with ModelObject and DataPropertyNames.

I have played around with these and have managed to make the errors go away, but I am not certain if it's right:

type SelectModelKeys<T> = Exclude<
  objection.NonFunctionPropertyNames<T>,
  | `$${string}`
  | 'QueryBuilderType'
  | 'foreignKeyId'
>

@puckey could you take a look?

lehni commented 1 year ago

Also, Objection just uses K instead of $Key, can / should we do the same?