1

How can I turn all class properties (excluding methods, ideally) into a type that represents an array of the class properties as strings?

This is what I have (the SomeTableFields type is what I'm trying to figure out).

// Objection.js Model
export class SomeTable implements Model {
  public static get tableName(): string {
    return 'someTable'
  }

  public id: string
  public name: string
  public age: int
}

// WHAT SHOULD THIS TYPE BE???
type SomeTableFields = (???)[]

// Function Definition
function getFromTable(id: string, fields?: SomeTableFields){
  ...
}

// Function Usage
getFromTable('1', ['id', 'name']) // should be OK
getFromTable('1', ['id', 'someNonExistentField']) // should be typecheck error

I tried (keyof SomeTable)[] but that results in Type 'string[]' is not assignable to type 'keyof SomeTable[]'. Wondered if I could do something like CastToString<keyof someTable>[] but I don't think that's a thing.

EDIT:

Guess I have a different problem, as @vera has pointed out that it is working. I also see it working if I use the type directly. But when I expose the function via this service and use it in another file I see the same Type 'string' is not assignable to type 'SomeTableFields'.`:

export interface SomeService {
  getFromTable: (id: string, fields?: SomeTableFields[]) => GetFromTableResponse
}

export async function createSomeService(): Promise<WorkspaceService> {
  return {
    getFromTable: (userId, workspaceId, selectFields) => getFromTable(id, selectFields)
  }
}
nonethewiser
  • 550
  • 1
  • 6
  • 18

1 Answers1

0

In exposing the function on a service and decalring the args, I added an extra [] here fields?: SomeTableFields[]. Removing it fixed it. Thank you @vera for verifying what I had and driving towards the real problem.

nonethewiser
  • 550
  • 1
  • 6
  • 18