triggerdotdev / trigger.dev

Trigger.dev is the open source background jobs platform.
https://trigger.dev/changelog
Apache License 2.0
9.05k stars 557 forks source link

bug: `AirtableFieldSet` does not expose type matching Airtable Formula fields with error #645

Closed martinse closed 12 months ago

martinse commented 1 year ago

Provide environment information

❯ npx envinfo --system --binaries

System: OS: macOS 13.5 CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz Memory: 34.34 MB / 32.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.8.0 - ~/.volta/tools/image/node/20.8.0/bin/node npm: 10.1.0 - ~/.volta/tools/image/node/20.8.0/bin/npm pnpm: 8.7.6 - ~/.volta/bin/pnpm Watchman: 2023.09.04.00 - /usr/local/bin/watchman

Describe the bug

AirtableFieldSet does not expose correct type for Airtable Formula fields with errors.

When creating a type/interface to represent an Airtable record with formulas - if the formula inside Airtable has errors, this cannot be typed through @trigger.dev/airtable.

E.g.:

type FormulaFieldType = string;
type FormulaFieldErrorType  = { error: string; };

export interface MyTable extends AirtableFieldSet {
    ID: number;
    MyFormulaField: FormulaFieldType | FormulaFieldErrorType
}

Gives:

TS2411: Property  MyFormulaField  of type  string | FormulaFieldErrorType  is not assignable to  string  index type
string | number | boolean | Collaborator | Collaborator[] | string[] | Attachment[] | undefined

The actual response data looks like this from within Trigger.dev:

[
  {
    "id": "recXXX",
    "fields": {
      "ID": 1,
      "ValidFormulaField": "Some value",
      "InvalidFormulaField": {
        "error": "#ERROR!"
      },
      ...
    }
  }
]

Reproduction repo

n/a

To reproduce

Additional information

Suggested fix: Add | {key: string} to AirtableFieldSet field type.

matt-aitken commented 1 year ago

@martinse do you happed to know if the official Airtable Node SDK supports this?

We're just wrapping their SDK and it's possible we've done something incorrect with the types we pass to their generic function.

martinse commented 1 year ago

Looking at the types from the official Airtable SDK I see the similar type def:

export interface FieldSet {
    [key: string]: undefined | string | number | boolean | Collaborator | ReadonlyArray<Collaborator> | ReadonlyArray<string> | ReadonlyArray<Attachment>;
}

Also reading their documentation at https://airtable.com/developers/web/api/field-model#formula it might be that they haven't defined the type correctly:

Cell format (read only)

string | number

I can't find any reference to this error object that I get back in their documentation. I was thinking perhaps Trigger.dev defined their own types due to the custom type in @trigger.dev/airtable but it seems as it just mirrors the official SDK type.

nicktrn commented 12 months ago

Just did some testing and you would have to extend that error type further, e.g. due to division by zero:

type FormulaFieldErrorType = { error: string } | { specialValue: "Infinity" | "NaN" };

As you verified, types are indeed incorrect in the official SDK.

Sadly, I can't recommend opening an issue in their repo. It seems largely unmaintained. There are still valid open issues from 5 years back that haven't received any attention.

I don't see a way to fix this nicely from our end - we'll likely have to resort to // @ts-ignore in a few places.