NotWoods / travel

Our collection of travel notes straight out of our notebook!
https://travel.tigerxdaphne.com
19 stars 2 forks source link

Formula property type possibly incorrect #3

Closed nlnw closed 1 month ago

nlnw commented 1 month ago

When I use a formula in my database, the property is available through:

post.data.properties.MyFormula.formula.string

But this gives a type error.

Property 'string' does not exist on type '{ type: "string"; string: string | null; } | { type: "date"; date: string | null; } | { type: "number"; number: number | null; } | { type: "boolean"; boolean: boolean | null; }'.
  Property 'string' does not exist on type '{ type: "date"; date: string | null; }'.ts(2339)
NotWoods commented 1 month ago

You'll need an extra check or custom property schema, since TypeScript isn't sure which formula your page has.

const property = post.data.properties.MyFormula.formula;
if (property.type === 'string') {
  return property.string
} else {
  throw new Error()
}