Skip to content

Use generic type for FormulaPropertyItemObjectResponse #654

Description

@believelody

This issue is a suggestion.

Describe the bug
When dealing with formula property, I noticed :

type FormulaPropertyResponse = StringFormulaPropertyResponse | DateFormulaPropertyResponse | NumberFormulaPropertyResponse | BooleanFormulaPropertyResponse;

This way, if I have 2 properties in my datasource (one string, other a date), typescript cannot give me the corresponding type.

To Reproduce
Node version: 24.11.1
Notion JS library version: 5.6.0

Steps to reproduce the behavior:

  • Have at least one formula property in database ;
  • Make a datasource query with notion sdk ;
  • Retrieve formula value

Expected behavior
Currently I use this workaround :

{
          id: result.id,
          slug:
            "string" in properties.slug.formula &&
            properties.slug.formula.string
              ? properties.slug.formula.string
              : ""
}

I have to manually check if formula contains the wanted value.

We can solve this by doing this way :

export type FormulaPropertyItemObjectResponse<T extends string | Date | number | boolean> = {
    type: "formula";
    formula: FormulaPropertyResponse<T>;
    object: "property_item";
    id: string;
};
type FormulaPropertyResponse<T extends string | Date | number | boolean> = T extends string ? StringFormulaPropertyResponse : T extends Date ? DateFormulaPropertyResponse : T extends number ? NumberFormulaPropertyResponse : T extends boolean  ? BooleanFormulaPropertyResponse : never;

Usage :

export type NotionArticleDTO = {
  slug: FormulaPropertyItemObjectResponse<string>;
  title: TitlePropertyItemObjectResponse;
  publishedAt: FormulaPropertyItemObjectResponse<Date>;
  status: StatusPropertyItemObjectResponse;
  category: SelectPropertyItemObjectResponse;
};

What do you think about that ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions