question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recommended way of typing records?

See original GitHub issue

What is the best practice of getting typed fields for my records? I have tried a few different approaches, but I haven’t found any straightforward way.

In listResult1, I’m limited by ListResult is not exported from the sdk In listResult2, the getList function is not generic.

import PocketBase, {ListResult, Record} from 'pocketbase'
const client = new PocketBase('http://127.0.0.1:8090');

interface MyItem extends Record {
    name: string,
    age: number
}

async function example() {
    // ListResult type is not exported
    const listResult1: ListResult<MyItem> = await client.records.getList("my-item", 1, 20)

    // getList() is not generic
    const listResult2 = await client.records.getList<MyItem>("my-item", 1, 20)

    // the age field resolves to "any" instead of "number"
    listResult1.items[0].age
    listResult2.items[0].age
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
ganigeorgievcommented, Sep 21, 2022

@mnorlin Using type assertion as suggested by @silberjan is the easiest way to have typed records at the moment.

The generics suggestion is a nice one, although it will be a little hacky since the generic type cannot be used directly as constructor, but it is doable and I’ll consider it for the next major release (v0.8.0) that will be shipped with the changes from https://github.com/pocketbase/pocketbase/issues/376.

10reactions
ganigeorgievcommented, Sep 21, 2022

It’s a little too verbose in my opinion. I think the namespaced approach (aka. client.records.getList<T>) is more readable and easier to use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's More Important in Typing - Speed or Accuracy
The eternal debate when it comes to typing is which is more important - speed or accuracy? In our mind the answer is...
Read more >
The World's Fastest Typists | Typing Lounge
Hank Torres (USA) holds the record as the world's fastest typist in hands-free typing achieving a speed of 83.09 seconds at the 2011...
Read more >
Typing Speed | Pedagogical Resources
The best way to reach this goal is with a good typing method. What Speed Target Should I Aim For? Although speed is...
Read more >
The Fastest Typists in the World Share their Typing Secrets
Learn how to type fast from the fastest typists in the world. Type like a champ with a Das Keyboard : https://www.daskeyboard.com/ Featured ......
Read more >
How to Type Faster: 12 Typing Tips and Techniques - Lifehack
It is also very important that you keep this position as you type. Ensure that your posture is good, and this way, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found