Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the correct types for Fields #367

Open
jeremyzilar opened this issue May 5, 2023 · 2 comments
Open

Get the correct types for Fields #367

jeremyzilar opened this issue May 5, 2023 · 2 comments

Comments

@jeremyzilar
Copy link

Do you all have examples for working with Airtable and typescript?
In my base, I have a ✅ checkbox field (which I assume would be boolean, right?).
I have a component that is pulling in data from a single record, using this type: RecordData<FieldSet>.

Why when I get the data for that field fields.completed, is it showing the type as the following?

(index) FieldSet[string]: string | number | boolean | Collaborator | readonly Collaborator[] | readonly string[] | readonly Attachment[] | undefined

I would expect that having set the field "type" specifically in the base, that it would return as that particular type.
Or maybe I am not defining it correctly?

Here is a simplified example:

const Record = ((props: RecordData<FieldSet>)=>{
  const { fields, id } = props
  console.log("fields.completed", fields.completed)
  const completed = fields.completed ? "yes, it is completed": "not completed"
  return <>{completed}</>
})
@dopry
Copy link

dopry commented Mar 21, 2024

It's not going to be very easy to introspect your instance and setup you specific types and fields for the airtable js team. Most likely you'll need to do what I did and build a wrapper around their primitives that cast their fieldsets to your domain model

@domdomegg
Copy link

FYI I made a wrapper around the airtable library that's called airtable-ts that enables you to use the field / column types in your code (and gives you clear runtime errors if people edit tables and things no longer line up). This sounds similar to what @dopry might have built, but as a more general package than can be usedby others.

It also fixes weirdness with checkboxes, e.g. unchecked checkboxes are not returned from the API at all (e.g. you'd get undefined trying to access it), but airtable-ts treats that as actually false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants