Recommended way of typing records?
See original GitHub issueWhat 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:
- Created a year ago
- Reactions:4
- Comments:11 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
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.