🍿 @lorenzopant/tmdb

Utility

Type guards and utility types exported by the package.

These are utility functions and types exported from @lorenzopant/tmdb for working with TMDB API responses.


Type Guards — Image Paths

These type guards check whether an object contains a specific image-path property with a non-null string value. They are useful when working with multi-media result types that may or may not carry a particular image field.

import { hasPosterPath, hasBackdropPath, hasProfilePath, hasStillPath, hasLogoPath } from "@lorenzopant/tmdb";

Each guard is generic over its input and narrows to T & { <field>: string }, so the input type survives the check. That matters in two places:

  • Filtering. Array.prototype.filter only applies its narrowing overload when the predicate type extends the element type. Because these guards intersect rather than replace, movies.filter(hasPosterPath) yields (Movie & { poster_path: string })[] — every other field is still there, and poster_path is string instead of string | undefined.
  • Unknown input. unknown & { poster_path: string } collapses to { poster_path: string }, so passing a raw, untyped API response still works exactly as before.

hasPosterPath

function hasPosterPath<T>(data: T): data is T & { poster_path: string };

Returns true when data is an object with a string poster_path property.

if (hasPosterPath(result)) {
	// result.poster_path is a string here
	const url = tmdb.images.poster(result.poster_path);
}

Filtering a typed list keeps the element type intact:

const { results } = await tmdb.movies.popular();

const withPosters = results.filter(hasPosterPath);
// (MovieResultItem & { poster_path: string })[]

for (const movie of withPosters) {
	console.log(movie.title, tmdb.images.poster(movie.poster_path));
}

hasBackdropPath

function hasBackdropPath<T>(data: T): data is T & { backdrop_path: string };

Returns true when data is an object with a string backdrop_path property.


hasProfilePath

function hasProfilePath<T>(data: T): data is T & { profile_path: string };

Returns true when data is an object with a string profile_path property.


hasStillPath

function hasStillPath<T>(data: T): data is T & { still_path: string };

Returns true when data is an object with a string still_path property.


hasLogoPath

function hasLogoPath<T>(data: T): data is T & { logo_path: string };

Returns true when data is an object with a string logo_path property.


On this page