首页 > 其他分享 >[Typescript] Using type predicates

[Typescript] Using type predicates

时间:2023-02-10 02:22:32浏览次数:36  
标签:Typescript undefined value Should values predicates Using type

 

import { expect, it } from "vitest";
import { Equal, Expect } from "../helpers/type-utils";

export const values = ["a", "b", undefined, "c", undefined];

const filteredValues = values.filter((value): value is string => Boolean(value));

it("Should filter out the undefined values", () => {
  expect(filteredValues).toEqual(["a", "b", "c"]);
});

it('Should be of type "string[]"', () => {
  type test1 = Expect<Equal<typeof filteredValues, string[]>>;
});

 https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

标签:Typescript,undefined,value,Should,values,predicates,Using,type
From: https://www.cnblogs.com/Answer1215/p/17107629.html

相关文章