#Trying to create a type by asserting the return value is not undefined.

8 messages · Page 1 of 1 (latest)

neon turret
#

Hi.
I am trying to create this type :

type TmdbMovie1 = RouterOutputs['tmdbRouter']['searchMovie']['data']['results']

from

{
    page: number;
    results: {
        adult?: boolean | undefined;
        backdrop_path?: string | undefined;
        genre_ids?: number[] | undefined;
        id?: number | undefined;
        original_language?: string | undefined;
        ... 8 more ...;
        vote_count: number;
    }[];
    total_pages: number;
    total_results: number;
} | undefined 

I am using openapi-typescript / openapi-fetch
But because data can apparently be undefined. I guess in the event of a server failure i cannot extract the type.
I just get property results does not exist on type.
How can create my required type?

rustic raft
dusk elbow
#

The payload is not complete but maybe you could abuse intersections like this: ```typescript
type TmdbMovie1 = (((RouterOutputs['tmdbRouter'] & {})['searchMovie'] & {})['data'] & {})['results']`

#

If RouterOutputs can also be undefined then you need another & {} with it.

neon turret
#

Thanks both.
the NonNullable seems ok though or is there any benefit to either solution

rustic raft
#

NonNullable does the same thing as & {} but it's clearer what the intention is

neon turret
#

Thanks @rustic raft

#

!resolved