如何使用打字稿从概念块中获取 "to_do"?
How to get "to_do" from a notion block using typescript?
最近我尝试了 Notion API 的最终版本。使用打字稿我做了一个检索块子请求:
(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
出于某种原因,typescript 告诉我 to_do
不存在于类型 (PartialBlockObjectResponse | BlockObjectResponse)
中。我查看了类型定义,然后……它就在那里:
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;
}
// ...
}
我尝试制作类型保护
function isToDo(value: PartialBlockObjectResponse | BlockObjectResponse): value is BlockObjectResponse {
return "to_do" in value;
} /* Error: TS2304: Cannot find name 'PartialBlockObjectResponse'. */
并从包中导入类型
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.
都没有帮助。
(沮丧地)那个包 does not export its types. There's an open issue 关于它在 repo 中。
但是您可以使用带有谓词函数的泛型来解决这个问题:
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;
}
*/
}
}
})()
最近我尝试了 Notion API 的最终版本。使用打字稿我做了一个检索块子请求:
(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
出于某种原因,typescript 告诉我 to_do
不存在于类型 (PartialBlockObjectResponse | BlockObjectResponse)
中。我查看了类型定义,然后……它就在那里:
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;
}
// ...
}
我尝试制作类型保护
function isToDo(value: PartialBlockObjectResponse | BlockObjectResponse): value is BlockObjectResponse {
return "to_do" in value;
} /* Error: TS2304: Cannot find name 'PartialBlockObjectResponse'. */
并从包中导入类型
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.
都没有帮助。
(沮丧地)那个包 does not export its types. There's an open issue 关于它在 repo 中。
但是您可以使用带有谓词函数的泛型来解决这个问题:
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;
}
*/
}
}
})()