Why Type Guards?
Many Notion endpoints return union types likePageObjectResponse | PartialPageObjectResponse. Type guards let you:
- Check if a response contains full details
- Safely access properties that only exist on full objects
- Get proper TypeScript type narrowing
Available Type Guards
All type guards are exported from the main package:Page Type Guards
isFullPage
Checks if a response is a fullPageObjectResponse.
true if response.object === "page" and has a url property.
Example:
src/helpers.ts:186-189
Block Type Guards
isFullBlock
Checks if a response is a fullBlockObjectResponse.
true if response.object === "block" and has a type property.
Example:
src/helpers.ts:176-180
Data Source Type Guards
isFullDataSource
Checks if a response is a fullDataSourceObjectResponse.
true if response.object === "data_source".
Example:
src/helpers.ts:194-198
isFullPageOrDataSource
Checks if a response is either a fullPageObjectResponse or DataSourceObjectResponse. Useful when working with search or query results.
src/helpers.ts:216-224
Database Type Guards
isFullDatabase
Checks if a response is a fullDatabaseObjectResponse.
true if response.object === "database".
Example:
src/helpers.ts:201-205
User Type Guards
isFullUser
Checks if a response is a fullUserObjectResponse.
true if the response has a type property.
Example:
src/helpers.ts:229-233
Comment Type Guards
isFullComment
Checks if a response is a fullCommentObjectResponse.
true if the response has a created_by property.
Example:
src/helpers.ts:237-242
Rich Text Type Guards
For rich text items, use these type guards to determine the content type:isTextRichTextItemResponse
true if richText.type === "text".
Example:
src/helpers.ts:247-251
isEquationRichTextItemResponse
true if richText.type === "equation".
Example:
src/helpers.ts:256-260
isMentionRichTextItemResponse
true if richText.type === "mention".
Example:
src/helpers.ts:265-269
Combining Type Guards
Type guards work well together for complex filtering:Best Practices
Always use type guards before accessing full properties
Always use type guards before accessing full properties
Properties like
url, created_time, and type-specific fields only exist on full objects. Use type guards to avoid runtime errors:Type guards enable autocomplete
Type guards enable autocomplete
After using a type guard, TypeScript will provide full autocomplete for the narrowed type, making development easier and catching errors early.
Handle both full and partial cases
Handle both full and partial cases
Always consider what to do when an object is partial:
Type Definitions
All type guard functions use TypeScript’sis operator for type predicates, which tells TypeScript to narrow the type in the if block: