Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
How to get "to_do" from a notion block using typescript?

Recently I tried final version of Notion API. Using typescript I made a retrieve block children request:

(async () => {
    const blockId = process.env.BLOCK_ID;
    const response = await notion.blocks.children.list({
        block_id: blockId,
        page_size: 50,
    });
    console.log(response.results[1].to_do);
})();

Error message

For some reason typescript tells me that to_do doesn't exist in type (PartialBlockObjectResponse | BlockObjectResponse). I looked at type definition and... it was there:

declare type BlockObjectResponse = {
    // ...
    {
    type: "to_do";
    to_do: {
        rich_text: Array<RichTextItemResponse>;
        color: ApiColor;
        checked: boolean;
    };
    object: "block";
    id: string;
    created_time: string;
    created_by: {
        id: IdRequest;
        object: "user";
    };
    last_edited_time: string;
    last_edited_by: {
        id: IdRequest;
        object: "user";
    };
    has_children: boolean;
    archived: boolean;
} 
// ...
}

I tried making type guard

function isToDo(value: PartialBlockObjectResponse | BlockObjectResponse): value is BlockObjectResponse {
     return "to_do" in value;
}  /* Error: TS2304: Cannot find name 'PartialBlockObjectResponse'. */

and importing type from package

import type {PartialBlockObjectResponse} from "@notionhq/client/build/src/api-endpoints"; 
// Error: Module '"@notionhq/client/build/src/api-endpoints"' declares 'PartialBlockObjectResponse' locally, but it is not exported.

Neither helped.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

(Frustratingly) That package does not export its types. There's an open issue about it in the repo.

But you can work around this using a generic with your predicate function:

TS Playground

import {Client} from '@notionhq/client';
declare const notion: Client;
declare const blockId: string;

function isTodo <T extends Record<string, unknown>>(obj: T): obj is T & { type: 'to_do' } {
  return 'type' in obj && obj.type === 'to_do';
}

(async () => {
  const response = await notion.blocks.children.list({block_id: blockId, page_size: 50});

  for (const result of response.results) {
    if (isTodo(result)) {
      result.to_do; /*
             ^^^^^
      is now this type:
      {
          rich_text: RichTextItemResponse[];
          color: ApiColor;
          checked: boolean;
      }
      */
    }
  }
})()

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda