#How can I let intellisense know that this variable has been type checked?

27 messages · Page 1 of 1 (latest)

velvet mural
#

I have for example this variable:

let moveReport: MoveReport.Interface[] = []

And I have this check function

export function hasMoveReportParsedDestination(moveReport: Interface): moveReport is Interface & { destination: Pick<LocationI, 'id' | 'name'> } {
        return typeof moveReport.destination !== 'string';
    }

if I do

moveReport = data.filter((moveReport) => MoveReport.hasMoveReportParsedDestination(moveReport))

How can I let typescript know that moveReport's destination is now of type Pick<LocationI, 'id' | 'name'> and not string anymore?

Because moveReport is still recognized as string |

drifting plume
#

wrapping it in a () => gets rid of the type predicate return type

velvet mural
#

@drifting plume The problem isn't there, I am trying to find a way to specify that

let moveReport: MoveReport.Interface[] = []

is actually

let moveReport: Array<MoveReport.Interface & { destination: destination: Pick<LocationI, 'id' | 'name'> }> = []

Without writting that each time.

The MoveReport.Interface already has
destination: string | destination: Pick<LocationI, 'id' | 'name'>

And I use hasMoveReportParsedDestination to check if that is true

But the main variable being declared as MoveReport.Interface[] doesn't know that I already checked the type and still suggests that it might be a string

So what is a better way to declare the type of the variable?

Keeping in mind that it will be assigned at a later time, so at the beginning it will be null

drifting plume
#

oh

#

that's not possible, variables can't change types

velvet mural
#

I understand, so I have to specify the & { destination: Pick... } each time

drifting plume
#

or create a new variable

#

or initialize it to the correct type

#

let moveReport: ReturnType<typeof MoveReport.hasMoveReportParsedDestination>[]

velvet mural
drifting plume
#

oh whoops... of course

velvet mural
#

should I make it return the parsed value?

#

then I could use that return type

drifting plume
#

maybe = [].filter(MoveReport.hasMoveReportParsedDestination)

drifting plume
#

not sure whether it's a better idea though

#

filter is pretty convenient

velvet mural
drifting plume
#

not 100% sure it works though

#

but if it does it'd be pretty sweet

velvet mural
#

Hmm, it returns type never[]

#

since the array is empty probably

#

I think I will create two separate types and union them, so I can use it easily across the app

#

Hmm but apparently I can't do this

type RawMoveReportDestination = { destination: string }
    type ParsedMoveReportDestination = { destination: Pick<LocationI, 'id' | 'name'> }

    export interface Interface extends (RawMoveReportDestination | ParsedMoveReportDestination) {
        id: number;
        payload: MoveReportPayloadI;
        note: string;
        author: AppUser;
        departureDate: string;
        arrivalDate: string;
        isArrivalEstimated: boolean;
        refType: refType;
        refId: number;
        arrivalConfirmedAt: string;
        confirmedBy: AppUser;
        departureLocation: LocationI;
    }

I get

An interface can only extend an identifier/qualified-name with optional type arguments.
#

I can only extend one of them

drifting plume
#

yeah