November 2018
Beginner
502 pages
10h 22m
English
One way we can perform type checking in a function is with another function that has a return type as a type predicate. Let's explore this and eventually create a new version of our logScores function:
const scoresCheck = ( scores: any): scores is { name: string; scores: number[] } => { return "name" in scores && "scores" in scores;};
This takes in a scores parameter that has a type predicate, scores is { name: string; scores: number[] }, ensuring it contains the correctly typed name and scores properties. The function simply returns whether the scores parameter contains the name and scores properties.
Read now
Unlock full access