【问题标题】:Notion API gives an error for type candidateNotion API 为候选类型提供错误
【发布时间】:2022-01-21 14:29:41
【问题描述】:

导致问题的代码是下面的代码

import {Client, LogLevel} from "@notionhq/client";

const notion = new Client({
    auth: process.env.NOTION_TOKEN,
    logLevel: process.env.NODE_ENV !== 'production' ? LogLevel.DEBUG : undefined
});

export const getPage = async (pageId:string) => {
    const response = await notion.pages.retrieve({ page_id: pageId });

    // ↓ Error this Line !!!
    const pageTitle = response.properties.Name.title[0].plain_text;
    console.log(pageTitle);
    return response;
};

当使用 notion-API 检索块时, 我收到与上述类似的错误,无法正确获取该属性。 该类型被视为any,这会导致编辑器出错,但可以在浏览器中正确检索。

如何消除编辑器中的错误?

这个问题也可能是编辑器或打字稿的问题。 请给我一些提示????️.

感谢您在我之前回答!

Property 'properties' does not exist on type 'GetPageResponse'.
  Property 'properties' does not exist on type '{ object: "page"; id: string; }'.ts(2339)

主环境是这样的

// package.json

{
  "name": "notion-api-test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "typelint": "tsc --noEmit"
  },
  "dependencies": {
    "@chakra-ui/icons": "^1.1.1",
    "@chakra-ui/react": "^1.7.4",
    "@emotion/react": "^11.7.1",
    "@emotion/styled": "^11.6.0",
    "@notionhq/client": "^0.4.12",
    "firebase": "^9.6.3",
    "framer-motion": "^5.6.0",
    "next": "12.0.8",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "recoil": "^0.5.2",
    "swr": "^1.1.2"
  },
  "devDependencies": {
    "@types/node": "^17.0.9",
    "@types/react": "^17.0.38",
    "@typescript-eslint/eslint-plugin": "^5.10.0",
    "@typescript-eslint/parser": "^5.10.0",
    "eslint": "8.7.0",
    "eslint-config-next": "12.0.8",
    "eslint-plugin-react": "^7.28.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "focus-visible": "^5.2.0",
    "prettier": "^2.5.1",
    "typescript": "^4.5.4"
  }
}

// vscode

Version: 1.63.2
Commit: 899d46d82c4c95423fb7e10e68eba52050e30ba3
Date: 2021-12-15T09:38:17.605Z
Electron: 13.5.2
Chromium: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Darwin arm64 21.1.0
node --version
v16.8.0

【问题讨论】:

    标签: reactjs typescript visual-studio-code next.js


    【解决方案1】:

    这是来自node_modules\@notionhq\client\build\src\api-endpoints.d.tsGetPageResponse 的类型定义:

    export declare type GetPageResponse = {
        parent: {
            type: "database_id";
            database_id: IdRequest;
        } | {
            type: "page_id";
            page_id: IdRequest;
        } | {
            type: "workspace";
            workspace: true;
        };
        properties: Record<string, {
            type: "title";
            title: Array<{
               ...
    

    乍一看,你可以看到它有properties: Record&lt;string, {...}&gt;,那是怎么回事,对吧?

    问题在于它只出现在该类型的两种可能定义之一中。查看这个简化版本:

    export declare type GetPageResponse = {
        parent: {/*...*/};
        properties: Record<string, {/*...*/}>;
        /* ... */
    } | {
        object: "page";
        id: string;
    };
    

    如您所见,GetPageResponse 也可以是{ object: "page"; id: string; } 类型,而该类型没有properties

    因此,您必须首先确保该属性存在,如下所示:

    if ("properties" in response) {
      const pageTitle = response.properties.Name.title[0].plain_text;
      console.log(pageTitle);
      return response;
    }
    

    完整的编译代码可能如下所示:

    import { Client, LogLevel } from "@notionhq/client";
    
    const notion = new Client({
      auth: process.env.NOTION_TOKEN,
      logLevel: process.env.NODE_ENV !== "production" ? LogLevel.DEBUG : undefined,
    });
    
    export const getPage = async (pageId: string) => {
      const response = await notion.pages.retrieve({ page_id: pageId });
    
      if ("properties" in response) {
        if ("title" in response.properties.Name) {
          const pageTitle = response.properties.Name.title[0].plain_text;
          console.log(pageTitle);
          return response;
        }
      }
    };
    

    【讨论】:

    • 感谢您的解决方案!该代码停止了错误!目前这是一种解决方法,但我想知道是否有办法调整设置或在没有此描述的情况下使其工作?
    • 这是另一个问题,请参阅Is there a way to relax TypeScript compiler warning TS2339?。 TL;DR,你可以使用as any,但这有点违背了使用 TypeScript 的目的。如果他们解决了您的问题,请将答案标记为已接受,谢谢!
    猜你喜欢
    • 2022-10-05
    • 2021-08-04
    • 2015-09-26
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多