官术网_书友最值得收藏!

Type checking with a type predicate

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:

  1. First, we'll define a new function called scoresCheck to do the necessary type checking: 
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.

  1. Let's use this function in our logScores function:
function logScores(scores: unknown) {
if (scoresCheck(scores)) {
console.log(scores.firstName);
console.log(scores.scores);
}
}

      We immediately get the compilation error we want:

The type predicate, scores is { name: string, scores: number[] }, allows the TypeScript compiler to narrow down the type in the if block that logs the properties to the console. This results in scores.scores compiling fine, but scores.firstName is giving an error, which is just what we want.

The type predicate is the key bit. Without it, the TypeScript compiler will still throw errors on the valid scores.scores reference. Try removing the type predicate and see for yourself.

Note that we can make the predicate a little more readable with a type alias:

type Scores = { name: string; scores: number[] }

const scoresCheck = (
scores: any
): scores is Scores => {
return "name" in scores && "scores" in scores;
};

Using a type predicate in this way is called a type guard. There are other ways of implementing type guards, which we'll cover later in the book.

主站蜘蛛池模板: 潼关县| 寿光市| 杭锦旗| 蓝田县| 吉林市| 成武县| 兰西县| 兰西县| 北川| 喀喇沁旗| 溧阳市| 岑溪市| 竹山县| 永济市| 广宁县| 隆回县| 昌黎县| 无为县| 成都市| 凤山县| 临颍县| 河东区| 滨州市| 海林市| 越西县| 铁力市| 宝应县| 吴旗县| 长顺县| 贡觉县| 宁河县| 巴林右旗| 大埔县| 双江| 宁乡县| 马龙县| 旬阳县| 东乌珠穆沁旗| 中方县| 马关县| 廉江市|